cookieChoices = {};
Showing posts with label Generic and Assemblies. Show all posts
Showing posts with label Generic and Assemblies. Show all posts

Thursday, 3 April 2014

Example on Create and Use of DLL in Csharp

Dynamic Linking Library:

  A DLL is an application which can have everything what an EXE can have.

 The difference between them is, an EXE can execute independently but DLL cannot.

 The code in the DLL can be reused in many other applications but the code in EXE cannot be reused.

Types of DLL in Windows OS:

 1. Win32 DLL: Here the code is available in the form of simple "C" functions.

 2. COM DLL: This DLL have code in the form of reusable COM Components. These are also referred as ActiveX DLL.

  3. .NET DLL: These DLL’s (Portable Executables) are used for distributing the reusable classes to various types of .NET applications.

About Namespaces

 1. A namespace is a logical collection of classes and other types with unique names. In a given namespace all the types have unique name and thus it is used to resolve the ambiguity in case of name conflict.

 2. Any type when used outside the namespace, it must be qualified by its namespace, but when used by another type with in the same namespace it need not be qualified by the namespace.

 3. We can use using Namespace on top of the file so that we don’t have to qualify all the types of that namespace in that file.

=========================================================================================

How to Create DLL in CSharp:


Open New Project à Select ClassLibrary à Name as “CACreateDllExampleApp”à

Rename Class1.cs to Math.cs

Copy below Code to Math.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CACreateDllExampleApp
{
    public class Math
    {
        public int sum(int a, int b)
        {
            return a + b;
        }

        public int diff(int a, int b)
        {
            return a - b;
        }

        public int product(int a, int b)
        {
            return a * b;
        }
    }
}

Build the Project.

A dll of name CACreateDllExampleApp.dll is generated in binàRelease folder.


How to Use DLL File in another Project

Create New Console Project

è  In Solution explorer Right Click on the Project à Add Reference à
è  Go to CACreateDllExampleApp Project Folder
è  In binàReleaseà you will find CACreateDllExampleApp.dll Add this reference to your current project

Copy the below code to Program.cs

Run the project.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CACreateDllExampleApp;

namespace CAUseDLLApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 10;
            int y = 5;

            CACreateDllExampleApp.Math obj = new CACreateDllExampleApp.Math();
            string sum = obj.sum(x, y).ToString();
            string diff = obj.diff(x, y).ToString();
            string product = obj.product(x, y).ToString();

            Console.WriteLine(sum);
            Console.WriteLine(diff);
            Console.WriteLine(product);
            Console.ReadLine();
        }
    }
}



Wednesday, 2 April 2014

Abstract Class Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAInheritance_AbstractApp
{
    abstract  class Figure
    {
        public int dimension;
        public abstract double Area();
        public abstract double Perimeter();
    }

    class Square : Figure
    {
        public override double Area()
        {
            return dimension * dimension;
        }

        public override double Perimeter()
        {
            return 4 * dimension;
        }
    }

    class Circle : Figure
    {
        public override double Area()
        {
            return Math.PI * dimension * dimension;
        }

        public override double Perimeter()
        {
            return 2 *Math.PI* dimension;
        }
    }

    class AbstractApp
    {
        static void Main(string[] args)
        {
            Figure fi = new Square();
            fi.dimension = 10;
            Console.WriteLine(fi.Area());
            Console.WriteLine(fi.Perimeter());
            Console.ReadLine();
        }
     
    }
}

Genric Class Example

First we create a normal integer stack class .

Example of   integer stack class below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAGenericExamples
{
    //Stack of Integer
    public class Stack
    {
        int[] data;
        int top = -1;

        public Stack(int size)
        {
            data = new int[size];
        }

        public void push(int value)
        {
            top++;
            data[top] = value;
        }

        public int pop()
        {
            int value = data[top];
            top--;
            return value;
        }

        public int GetTopElement()
        {
            return data[top];
        }

        public void print()
        {
            for (int i = 0; i <= top; i++)
            {
                Console.WriteLine(data[i]);
            }
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Stack s = new Stack(5);
            s.push(5);
            s.push(12);
            s.print();
            int n = s.pop();
            Console.WriteLine(n);
            s.print();
            Console.ReadLine();
        }
    }
}

==============================================================

Next we create a normal object stack class .

Example of   object stack class below: it is Non Generic Stack Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAGenericExamples
{
    class NonGenericStackClass
    {
        object[] data;
        int top = -1;

        public NonGenericStackClass(int size)
        {
            data = new object[size];
        }

        public void push(object value)
        {
            top++;
            data[top] = value;
        }

        public object pop()
        {
            object value = data[top];
            top--;
            return value;
        }

        public object GetTopElement()
        {
            return data[top];
        }

        public void print()
        {
            for (int i = 0; i <= top; i++)
            {
                Console.WriteLine(data[i]);
            }
        }
    }

    class NonGenericProgram
    {
        static void Main(string[] args)
        {
            NonGenericStackClass s = new NonGenericStackClass(5);
            s.push(5);
            s.push("CSharp");
            s.push(12);
            s.push(new Stack(5));
            s.print();
            Console.WriteLine(s.pop());
            s.print();
            Console.ReadLine();
        }
    }

}

=========================================================

From Above Two Example we know the difference between a specific datatype class and an object class.

Now we Create a generic Class of same above stack class for clear understanding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAGenericExamples
{
    class GenericClass<T>
    {
        T[] data;
        int top = -1;

        public GenericClass(int size)
        {
            data = new T[size];
        }

        public void push(T value)
        {
            top++;
            data[top] = value;
        }

        public T pop()
        {
            T value = data[top];
            top--;
            return value;
        }

        public T GetTopElement()
        {
            return data[top];
        }

        public void print()
        {
            for (int i = 0; i <= top; i++)
            {
                Console.WriteLine(data[i]);
            }
        }

    }

    class GenericProgram
    {
        static void Main(string[] args)
        {
            GenericClass<int> s = new GenericClass<int>(5);
            s.push(5);
            //s.push("CSharp"); ERROR Comes here if un comment
            s.push(12);
            s.print();
            Console.WriteLine(s.pop());
            s.print();

            GenericClass<string> ss = new GenericClass<string>(2);
            ss.push("CSharp");
            ss.push("Demo");
            ss.print();

            Console.ReadLine();
        }
    }

}

=========================================================

From the Above generic Class as example ,, we create a generic Collection class example below 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAGenericExamples
{

    class student
    {
        public int id;
        public string name;

        public student(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }

    class GenericCollectionClass
    {
        static void Main(string[] args)
        {
            List<student> s = new List<student>();
            s.Add(new student(1, "s1"));
            s.Add(new student(2, "s2"));
            s.Add(new student(3, "s3"));
            s.Add(new student(4, "s4"));
            foreach (student ss  in s)
            {
                Console.WriteLine(ss.id +" "+ ss.name);
            }
            Console.WriteLine();

            /// Other way of printing student details collection

            IEnumerator<student> en = s.GetEnumerator();
            while (en.MoveNext())
            {
                student st = en.Current;
                Console.WriteLine(st.id +" "+st.name);
            }

            Console.ReadLine();
        }
    }
}