动态或非动态 [英] Dynamic or not dynamic

查看:73
本文介绍了动态或非动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我遇到过一些关于c#中使用的不同变量的材料,但我不确定我是否完全理解它然而。我试图明确地理解这两个:



  dynamic  obj; 





  object  obj; 





这两者有什么区别?

他们究竟做了什么?

如何以及何时使用动态值?



提前感谢您的帮助。 :)

解决方案

欢迎使用CodeProject!



你的问题告诉我你需要带一些研究.NET和C#的基础知识,基础知识。谢尔盖上面回答的链接是你学习的非常好的资源。



我建议你下载优秀的(免费)书籍.NET Book Zero Charles Petzold:[ ^ ],并仔细检查。



对象类型:.NET中的每个类都继承自名为object的根结构/类型。这意味着什么:它意味着存在一个用于分配内存的模板......指针和其他信息......每个类的创建都是基础,是创建任何类实例的基础。注意:.NET中的值类型继承自继承自Object的ValueType。



是的,对象类型的其他方面以及它如何工作, 但那些可以等到以后。在未来你可以深入研究:堆栈,堆,ValueType与ObjectType,装箱/解拳等。



动态类型(C#> = 4 ):C#是一种强类型语言,编译器必须在编译时知道...每个字段的类型,每个类,每个属性,方法可以接受的每个对象作为参数,或作为结果。



说C#要求早期绑定编译也是正确的。



但是,在某些情况下,人们会想要后期绑定,推迟编译器的内存和行为分配......在运行时处理Type ...基于Type ...为什么? ...因为您可能有想要处理不同类型的对象,并且在编译时您的程序不知道这些类型。



有一个使用System.Dynamic命名空间在C#中使用后期绑定的价格:运行时编译器必须使用其名称解析Type;内存的分配和构造所需的内部状态与每个新类型您的动态结构或字段已分配给它。方法调用会比较慢。



关于后期绑定和System.Dynamic的好文章,其中一位MS人员在C#中实现它:[< a href =http://blogs.msdn.com/b/cburrows/archive/2008/10/27/c-dynamic.aspxtarget =_ blanktitle =New Window> ^ ] 。



现在:让我们看看你忙,学习,编码,分析你的代码及其行为,学习如何调试。


< blockquote>请看我对这个问题的评论。



请参阅:

https://msdn.microsoft.com/en-us/library/dd264736.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/dd264741.aspx [ ^ ],

https:// msdn。 microsoft.com/en-us/library/dd233052.aspx [ ^ ]。



正如您所看到的,有很多东西需要理解。如果你想知道其他非动态类型是如何工作的,你将不得不学习.NET的所有基础知识,我非常鼓励这样做。



关于差异的问题是完全错误的,以及大多数其他差异问题。 (如果你不是很明显,请告诉我们苹果和苹果之间的区别:-))。对他们究竟做了什么这个问题的答案,如果不是一本书,就会写一篇大文章,但对事物的理解可能会有所不同,从阅读上面引用的MSDN文章到学习.NET的主要部分,CLR和CLI。



-SA


Hi everyone,

I have come across some material that talks a bit about different variables that are used in c# but i am not sure that i fully understand it yet. I am trying to understand these two specifically:

dynamic obj;


and

object obj;



What are the differences between the two?
What exactly do they do?
How and when is a dynamic value used?

Thanks for the help in advance. :)

解决方案

Welcome to CodeProject !

Your question suggests to me that you need to take some to study the essentials, the basics, of .NET and C#. The links in the answer by Sergey above are very good resources for your study.

I suggest you download the excellent (free) book, ".NET Book Zero" by Charles Petzold: [^], and go through it carefully.

Object Type: Every Class in .NET inherits from the root structure/Type named 'object. What does that mean: it means there is a template for the allocation of memory ... a pointer and other information ... fundamental to the creation of every Class, fundamental to the creation of instances of any Class. Note: Value Types in .NET inherit from ValueType which inherits from Object.

Yes, there are other aspects of what the Object Type is, and how it "works," but those can wait until later. In the future you can study in depth: stack, heap, ValueType vs. ObjectType, boxing/un-boxing, etc.

Dynamic Type (C# >= 4): C# is a strongly typed language where the Compiler must "know" ... at compile time ... the Type of each field, each Class, each Property, each object a method may accept as a parameter, or return as a result.

It is also correct to say C# demands early-binding to compile.

But, there are situations where one would want late-binding, to defer the compiler's allocation of memory and behavior ... to handle the Type at run-time ... based on Type ... why ? ... because you may have objects that you wish to deal with of different Types, and those Types are not known to your program at compile time.

There is a "price" for using late-binding in C# by using the System.Dynamic namespace: the compiler at run-time must resolve the Type using its Name; the allocation of memory and construction of the required internal state with each "new" Type your dynamic structure, or field, has assigned to it. Method calls will be slower.

Good article on late-binding and System.Dynamic here by one of the people at MS who worked on implementing it in C#: [^].

Now: let's see you get busy, studying, coding, analyzing your code and its behaviors, learning how to debug.


Please see my comment to the question.

Please see:
https://msdn.microsoft.com/en-us/library/dd264736.aspx[^],
https://msdn.microsoft.com/en-us/library/dd264741.aspx[^],
https://msdn.microsoft.com/en-us/library/dd233052.aspx[^].

As you can see, there is a whole lot of stuff to understand. And if you want to know how other, non-dynamic types work, you will have to learn pretty much all the basics of .NET, which I would highly encourage to do.

The question about "difference" is totally incorrect, as well as most other "difference" questions. (If it's not quite obvious to you, please tell us the difference between apple and Apple :-)). And an answer to the question "what exactly they do" make take a whole big article, if not a book, but understanding of things may take something ranging from reading of the MSDN articles referenced above to learning major part of .NET, CLR anc CLI.

—SA


这篇关于动态或非动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆