Dynamic Data Type in C#
The dynamic keyword brings exciting new features to C# 4. Dynamic Type means that you can store any type of value in the dynamic data type variable because type checking for dynamic types of variables takes place at run-time. In order to use the dynamic type in C#, you use the dynamic keyword in place of another type name.
The key to the dynamic type is that the type checking is disabled until run-time. When you use the dynamic keyword you tell the compiler to turn off compile-time checking . So, it is not type safe i.e. Compiler doesn't have any information about the type of variable. This can produce problems if the run-time type does not support the members that are called.
The reason is that the compiler doesn't know the runtime type of the object and therefore can't tell you that the increment operation is not supported in this case. Absence of compile-time type checking leads to the absence of IntelliSense as well. Because the C# compiler doesn't know the type of the object, it can't enumerate its properties and methods. This problem might be solved with additional type inference , as is done in the IronPython tools for Visual Studio, but for now C# doesn't provide it.
Dynamic type as Parameter
Dynamic type can be passed as method argument so that it can accept any type of parameter at run time.
Casting is not required but you need to know the properties and methods related to stored type. It is effectively inherit the properties and methods appropriate to their type at runtime. The following code will run because str is a string at the point the Length property is used, for example:
In most situations it would be inadvisable to use the dynamic type unless you are integrating with a dynamic language or another framework where types are not known at compile time. Because the compiler does not know what type the dynamic variable will eventually become, so it’s unable to offer method or property code hints in Visual Studio.
Value Type and Reference Type
The Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. More on... Value Type and Reference Type
- Does C# support multiple Inheritance ?
- What is Process ID ?
- How do I make a DLL in C# ?
- How many ways you can pass values to Windows Services ?
- Can we use "continue" statement in finally block ?
- What is nullable type in c# ?
- Difference between the Debug class and Trace class ?
- What is lock statement in C#
- What is the difference between is and as operator in C#?
- What are circular references in C#?
- What are the differences between events and delegates in C#
- Explain the types of unit test cases in C#?
- How many types of comments are there in C#?
- What are the various ways to pass parameters to a method in C#?
- What are the different ways to deploy a assembly in net?
- What does assert() method do in c#
- What is literals in C# - Constants and Literals
- What is the use of goto statement in C#
- How can JIT code be faster than AOT compiled code
- Why events have no return types in .NET
- What's the difference between a static method and a non-static method in C#
- What's a weak reference c#?
- What is C# equivalent of the vb.net isNothing function
- What are indexers in C#
- What are generics in c#