Dynamic Keyword in C#

    Dynamic Keyword    

    In C# 4.0 dynamic keyword was introduced, the advantage of dynamic keyword is that the actual type of variable of dynamic type is decided at run time. You can initialize the variable of dynamic type with any type of value or even if you do not initialize the value then also it will not give any error.

    You can also change the value of a dynamic variable to any other type, e.g. if you assign a integer value to a dynamic variable at the time of initialization then later you can also change it to string type of value and you will not get any error.

    Let us look at an example of dynamic type of variable below.

As you can see in the above example we have declared a dynamic type variable a and we have not initialized the variable, then we assigned the integer value to it and then later assigned a string type of value, when we build the code we did not get any error and the build has been succeeded.

    Dynamic keyword is very useful when working with LINQ queries as we can store the output of the LINQ query in the dynamic keyword.

    Difference Between dynamic Keyword and var Keyword .

Dynamic

 

Var

dynamic is a dynamically typed variable.

             

var is a statically typed variable.

dynamic typed variable is not mandatory to initialize at the time of declaration. 

 

You need to assign a value to var type of variable at the time of declaration else you will get a compile time error.

 

You can also change the value of dynamic variable to any other type other than the type of value which was assigned before.

 

You cannot assign the value of any other type to var variable other than the type of value which was assigned when it was initialized else you will get a compile time error.

 

You can use dynamic type as a method return type and as a type of parameter passed to a method.

 

You cannot use var type of variable as a method return type or parameter passed to a method.

dynamic keyword was introduced in C# 4.0.

           

var keyword was introduced in C# 3.0.

    So we have looked in this article how we can use dynamic keyword in C#, and also the difference between dynamic and var keyword, I hope you find this article useful.

    For more information on C#, .NET please read my other articles. 

    If you have any queries related to C#, .NET or to provide any feedback, please write a mail on prson94@gmail.com and I will try to get back to you as soon as possible.

    If you like the article then please write it in the comments box as it will motivate me to write more articles.

    Thank You...….

Sealed Class in C#

Sealed Class


    Sealed classed can not be inherited, if you try to inherit from a sealed class then you will get a compile time error. Sealed classes are declared using sealed keyword as shown below.

public sealed class sealedClassDemo
    {
       
    }

    As shown in the below example we are trying to inherit from a sealed class and on the same time we can see a error message saying that we can not inherit from a sealed class.

    


    The most common use of sealed class is to stop the inheritance but you can still extend the sealed class using extension methods. So as shown in the below example we can extend the sealed class by having a extension method, to know more about extension methods please read my blog on extension methods in C#.

    


     So today we have discussed regarding sealed class in C# and there importance, how sealed keyword can be used.

    Thank You for taking your time and reading my post on sealed class, please read my other posts related to C#, jQuery and .NET.

    If you have any other questions related to .NET or C# then please write them in the comments section or write a mail on prson94@gmail.com and I will try to get back to you as soon as possible.

Class and Object in C#

 Class and Object

    Class in C# contains different data members and functions/methods and it binds them together. We can access the data members and functions of a class using objects of a class. Class is nothing but a blueprint of a object. You will better understand class by looking at following example.

    public class Test
    {

            //Data Members of a class

            int a, b;

            public void foo()
            {

                    // Method implementation
             
            }
    }    
  
As shown in the above example, we have created a class called Test and to define a class we have used class keyword. The class contains two data members as a and b and one function foo. We can access those data members and function by creating object of the class. We have used public access modifier for the class so that we can access the class outside of the class. By default access modifier for a class is internal. We can create an object of the class using new keyword as shown below.

        Test tst = new Test();

We can initialise the data members of the class using constructors in C#, to know more about please read my blog on constructors here.

So we have discussed about class and objects in C# today, please read my other blogs on .NET for more information.

For any issues related to C#, ASP.NET, jQuery or to provide any feedback please write to me on prson94@gmail.com, I will try my best to reply you as soon as possible.