var 与 dynamic的不同
来源:互联网 发布:瑜伽 书籍 知乎 编辑:程序博客网 时间:2024/06/10 19:27
Introduced in C# 3.0
Introduced in C# 4.0
Statically typed – This means the type of variable declared is decided by the compiler at compile time.
Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.
Need to initialize at the time of declaration.
e.g., var str=”I am a string”;
Looking at the value assigned to the variable str
, the compiler will treat the variable str
as string.
No need to initialize at the time of declaration.
e.g., dynamic str;
str=”I am a string”;
//Works fine and compiles
str=2;
//Works fine and compiles
Errors are caught at compile time.
Since the compiler knows about the type and the methods and properties of the type at the compile time itself
Errors are caught at runtime
Since the compiler comes to about the type and the methods and properties of the type at the run time.
Visual Studio shows intellisense since the type of variable assigned is known to compiler.
Intellisense is not available since the type and its related methods and properties can be known at run time only
e.g., var obj1;
will throw a compile error since the variable is not initialized. The compiler needs that this variable should be initialized so that it can infer a type from the value.
e.g., dynamic obj1;
will compile;
e.g. var obj1=1;
will compile
var obj1=” I am a string”;
will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 1 was assigned to it. Now assigning a string value to it violates the type safety.
e.g. dynamic obj1=1;
will compile and run
dynamic obj1=” I am a string”;
will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value “I am a string” was assigned to it.
This code will work fine.
- var 与 dynamic的不同
- var与dynamic区别
- var与dynamic区别
- dynamic与var
- 【C#4.0】中的dynamic与var的区别
- C#中的Object、Dynamic与var的区别
- dynamic和var的区别
- var和dynamic的区别
- var, object, dynamic的区别及使用
- dynamic、object、var 之间的区别
- var, object, dynamic的区别以及使用
- iOS——@synthesize 与@dynamic 的不同
- var 与 不var 的区别
- 【.Net码农】var, object, dynamic的区别以及使用
- .NET中dynamic和var的区别有哪些
- C# var和dynamic的用法和理解
- ES6 let和var的四个不同
- Hibernate的dynamic-insert与dynamic-update
- linux-2.6.14内核移植
- linux上很方便的上传下载文件工具rz和sz
- 2014-11-04 我的牙呦!
- 浮点型数据存储方式
- RedHat 6.5 x64 装 IBM Installation Manager X86
- var 与 dynamic的不同
- Iframe使用总结
- 关于IOC和DI的理解
- wsdl范例
- Android Studio 快捷键记录(比对Eclipse)
- eclipse启动时一直卡在android sdk content loader解决办法
- jquery 跨域 JSON和JSONP
- 任时间匆匆流去,我只在乎你
- Android开发入门实例:四则混合运算计算器