clr

Does it make sense to define a struct with a reference type member?

戏子无情 提交于 2019-12-17 10:41:14
问题 Is there any sense in defining a struct with a reference type member (and not defining it as a class)? For example, to define this struct: public struct SomeStruct { string name; Int32 place; } I asking because I know that a struct is a value type, and to define in it some reference type does not make any sense. Am I right? Can someone explain this? 回答1: Nine times out of ten, you should be creating a class rather than a structure in the first place. Structures and classes have very different

Create empty C# event handlers automatically

半腔热情 提交于 2019-12-17 10:15:01
问题 It is not possible to fire an event in C# that has no handlers attached to it. So before each call it is necessary to check if the event is null. if ( MyEvent != null ) { MyEvent( param1, param2 ); } I would like to keep my code as clean as possible and get rid of those null checks. I don't think it will affect performance very much, at least not in my case. MyEvent( param1, param2 ); Right now I solve this by adding an empty inline handler to each event manually. This is error prone, since I

variable scope in statement blocks

爷,独闯天下 提交于 2019-12-17 09:56:06
问题 for (int i = 0; i < 10; i++) { Foo(); } int i = 10; // error, 'i' already exists ---------------------------------------- for (int i = 0; i < 10; i++) { Foo(); } i = 10; // error, 'i' doesn't exist By my understanding of scope, the first example should be fine. The fact neither of them are allowed seems even more odd. Surely 'i' is either in scope or not. Is there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this? Or is just a case of

calling managed code from unmanaged code

时光怂恿深爱的人放手 提交于 2019-12-17 09:43:54
问题 I want to call my .NET code from unmanaged C++. My process entrypoint is .NET based, so I don't have to worry about hosting the CLR. I know it can be done using COM wrappers for .NET objects, but I would like to access individual static methods of managed classes, so COM isn't my shortest/easiest route. Thanks! 回答1: Assuming you are talking about real unmanaged code - not just native C++ running in a mixed-mode assembly compiled with /clr - the easiest way is to create a wrapper to your .NET

Is the CLR a virtual machine?

一世执手 提交于 2019-12-17 08:13:32
问题 I read a book which referred to the .net CLR as a virtual machine ? Can anyone justify this? What is the reason we need the concept of virtual machines on some development platforms? Isn't it possible to develop a native framework [one without virtual machine] that is fully object oriented and as powerful as .net? The book which refers to CLR as virtual machine is " Professional .Net Framework 2.0 ". 回答1: There are a lot of misconceptions here. I suppose you could think of .Net as a virtual

Is the CLR a virtual machine?

为君一笑 提交于 2019-12-17 08:12:26
问题 I read a book which referred to the .net CLR as a virtual machine ? Can anyone justify this? What is the reason we need the concept of virtual machines on some development platforms? Isn't it possible to develop a native framework [one without virtual machine] that is fully object oriented and as powerful as .net? The book which refers to CLR as virtual machine is " Professional .Net Framework 2.0 ". 回答1: There are a lot of misconceptions here. I suppose you could think of .Net as a virtual

How is GetHashCode() implemented for Int32?

我的梦境 提交于 2019-12-17 07:51:43
问题 I've been looking all over the place, but I can't find anything. Can anyone shed some light on this? 回答1: According to Reflector: public override int GetHashCode() { return this; } Makes sense, does it? 回答2: Best way to hash 32 bit value to 32 bit is not to invent wheel, use value itself. Very fast, no collisions, indeed a perfect way. 来源: https://stackoverflow.com/questions/3893782/how-is-gethashcode-implemented-for-int32

Should we always include a default constructor in the class?

只谈情不闲聊 提交于 2019-12-17 04:23:34
问题 I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not? Example public class Foo { Foo() { } Foo(int x, int y) { ... } } I am also interested to get some lights on this from experts. 回答1: You have to keep in mind that if you don't provide an overloaded constructor, the compiler will generate a default constructor for you. That means, if you just have public class Foo { } The compiler will generate this as:

Module initializers in C#

耗尽温柔 提交于 2019-12-17 04:06:25
问题 Module initializers are a feature of the CLR that are not directly available in C# or VB.NET. They are global static methods named .cctor that are guaranteed to run before any other code (type initializers, static constructors) in an assembly are executed. I recently wanted to use this in a project and hacked together my own solution (console program/msbuild task) using Mono.Cecil, but I was wondering: Is there any way to trick the C# compiler into emitting module intializers? Any attributes

Why is stack size in C# exactly 1 MB?

左心房为你撑大大i 提交于 2019-12-17 03:48:05
问题 Today's PCs have a large amount of physical RAM but still, the stack size of C# is only 1 MB for 32-bit processes and 4 MB for 64-bit processes (Stack capacity in C#). Why the stack size in CLR is still so limited? And why is it exactly 1 MB (4 MB) (and not 2 MB or 512 KB)? Why was it decided to use these amounts? I am interested in considerations and reasons behind that decision . 回答1: You are looking at the guy that made that choice. David Cutler and his team selected one megabyte as the