Posted in .Net Basics, C# 4.0

Abstract Classes in .Net

An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

Its a special type of class that cannot be instantiated. (So why we need that ??) There are some reasons, an Abstract class is only to be sub-classed. That means it enforces some contracts or say methods to all the subclasses.

Here is a basic sample of abstract class and how to access their abstract and non abstract methods.

abstract class testDemo
    {
        public void callme() // non abstract method
        {
            // Some implementation
        }

        abstract public void ImplementMe(); // abstract method
    }

    class Child : testDemo
    {
        //implement the abstract method
        public override void ImplementMe()
        {
            throw new NotImplementedException();
        }
    }

This is how you can access methods of the abstract class.

 public void TestMethod()
        {
            Child obj = new Child();
            obj.callme();       // call to a non abstract method
            obj.ImplementMe();  // call to an abstract method which is implemented on the Child class
        }

An abstract class can also have abstract properties as well, derived class has to implement this property.

An Abstract class cannot be a sealed class. Following is not valid

  abstract sealed class testDemo {}

An abstract Method cannot be Private. Access modifier of the abstract method must be the same in both abstract class and its derived class. Otherwise it will be detected as error by compiler.

private abstract int myProp(); // incorrect

An Abstract method can’t be virtual as it is implicitly virtual.

An abstract member can’t be static but an abstract method can be a static one.

Hope this help someone.

Posted in .Net Basics, C# 4.0

dynamic type in C# 4.0

The type dynamic was introduced with C# 4.0 long back but I have used this very recently in one of my project, so just want to share my experience with this wonderful feature of C# 4.0.

dynamic

The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. It behaves like type object in most circumstances, however the operations that contain expressions of type dynamic are not resolved or type checked by the compiler.

Variable of type dynamic is compiled into variable of type object, and that is why the dynamic type is exists at compile time but not at runtime.

How to use dynamic – a simple example:

class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

output:

System.Int32
System.Int32

you will see the output returns the same type as both the variables are having int value assigned. If you want to see the difference between dyn and obj at compile time, try following lines and compile the code.

dyn = dyn + 3;
obj = obj + 3;

The compiler raised an error for attempted addition of an integer and an object in expression obj + 3. But, no error is reported for dyn + 3. The expression that contains dyn is not checked at compile time because the type of dyn is dynamic.

For more information about the dynamic type http://msdn.microsoft.com/en-us/library/dd264741(VS.100).aspx and Using Type dynamic