Tuesday 11 February 2014

C# tutorial - Types

In this post we are going to see about types in c#.There are two kinds of types in C# value types and Reference types. Variables of value types directly contain the data.but in reference types in store the reference of the data.

C# value types are divided in to simple types, enum types, struct types, and nullable types.
C# reference types are divided in to class types, interface types, array types and delegate types.

Category
Description
Value
types
Simple types
Signed integral: sbyte, short, int, long
Unsigned integral: byte, ushort, uint, ulong
Unicode characters: char
IEEE floating point: float, double
High-precision decimal: decimal
Boolean: bool
Enum types
User-defined types of the form enum Enu { }
Struct types
User-defined types of the form struct Str {}
Nullable types
Extensions of all other value types with a null value
Reference
types
Class types
Ultimate base class of all other types: object
Unicode strings: string
User-defined types of the form class Copy {...}
Interface types
User-defined types of the form interface Itest {...}
Array types
Single- and multi-dimensional, for example, int[] and int[,]
Delegate types
User-defined types of the form e.g. delegate int Del(...)



from this post you can learn the types present in c#.

Sunday 9 February 2014

C# Tutorial - Polymorphism

In this article we are going to see about detailed information of polymorphism, Polymorphism means many forms, There are two types of polymorphism

1. static polymorphism - decide at compile time itself
2. dynamic polymorphism - decide at runtime

Following is the class we are going to use in that, 

static polymorphism :- Speech method is overload with different parameters
dynamic polymorphism :- animal class method voice is override in derived class dog, cat.

class Animal
    {
        public void Speech()
        {
            Console.WriteLine("Testing");
        }

        public void Speech(string word)
        {
            Console.WriteLine(word);
        }

        public void Speech(string word, int nooftimes)
        {
            for (int i = 0; i < nooftimes; i++)
            {
                Console.WriteLine(word);
            }
        }

        public virtual void Voice()
        {
            Console.WriteLine("No Voice");
        }
    }

    class Dog:Animal
    {
        public override void Voice()
        {
            Console.WriteLine("Bark");
        }
    }

    class Cat:Animal
    {
        public override void Voice()
        {
            Console.WriteLine("Meow");
        }
    }

static polymorphism :-
 Binds the correct function to execute at the compile time itself known as early binding or static binding

public void Speech()
        {
            Console.WriteLine("Testing");
        }

        public void Speech(string word)
        {
            Console.WriteLine(word);
        }

        public void Speech(string word, int nooftimes)
        {
            for (int i = 0; i < nooftimes; i++)
            {
                Console.WriteLine(word);
            }
        }


operator overloading
function overloading

dynamic polymorphism :-
 Binds the correct function to execute at the runtime.

public override void Voice()
{
    Console.WriteLine("Bark");
}

public override void Voice()
{
    Console.WriteLine("Meow");
}

Program

    class Program
    {
        static void Main(string[] args)
        {

            Animal anim = new Animal();

            /* Static polymorphism  */
            anim.Speech();
            anim.Speech("Raj");

            /* Dynamic polymorphism */
            Dog d = new Dog();
            Cat c = new Cat();

            anim.Voice();
            d.Voice();
            c.Voice();

            Console.Read();

        }
    }

    class Animal
    {
        public void Speech()
        {
            Console.WriteLine("Testing");
        }

        public void Speech(string word)
        {
            Console.WriteLine(word);
        }

        public void Speech(string word, int nooftimes)
        {
            for (int i = 0; i < nooftimes; i++)
            {
                Console.WriteLine(word);
            }
        }

        public virtual void Voice()
        {
            Console.WriteLine("No Voice");
        }
    }

    class Dog:Animal
    {
        public override void Voice()
        {
            Console.WriteLine("Bark");
        }
    }

    class Cat:Animal
    {
        public override void Voice()
        {
            Console.WriteLine("Meow");
        }
    }




output
*****
Testing
Raj
No Voice
Bark
Meow



From this post you can understand about polymorphism.