clr

Should we always include a default constructor in the class?

十年热恋 提交于 2019-11-26 18:38:23
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. 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: public class Foo { public Foo() { } } However, as soon as you add the other constructor public class Foo {

How are String and Char types stored in memory in .NET?

穿精又带淫゛_ 提交于 2019-11-26 18:28:54
问题 I'd need to store a language code string, such as "en", which will always contains 2 characters. Is it better to define the type as "String" or "Char"? private string languageCode; vs private char[] languageCode; Or is there another, better option? How are these 2 stored in memory? how many bytes or bits for will be allocated to them when values assigned? 回答1: How They Are Stored Both the string and the char[] are stored on the heap - so storage is the same. Internally I would assume a string

Simplest way to make cross-appdomain call?

我们两清 提交于 2019-11-26 18:28:29
问题 I need to call a method of object in another appdomain (pass param and get result). Ideas? UPD both AppDomain's are created not by my code (host app creates it, and then my code gets called). How I can get access to one AppDomain from another? 回答1: If you created an object in another domain e.g. with AppDomain.CreateInstanceAndUnwrap, all you need to call the object in another domain is to call an object's method. The simplest way to make a cross-application domain call is just to make a call

When passing a managed byte[] array through PInvoke to be filled in by Win32, does it need to be pinned?

筅森魡賤 提交于 2019-11-26 18:18:00
问题 Suppose you're calling a Win32 function that will fill in your byte array. You create an array of size 32, empty. Then pass it in to the Win32 function to be filled int, and use it later in your managed code. Does there exist the chance that the byte array might be moved or overwritten in between the time it was allocated and it is filled in by the Win32 function? 回答1: Short Answer: No, pinning is not necessary in this case Longer Answer: The CLR will automatically pin references to managed

Why does C#/CLR not support method override co/contra-variance?

给你一囗甜甜゛ 提交于 2019-11-26 17:51:53
问题 There are quite a few questions & answers about hacking around the limitation of C# not allowing method return (and argument) types to be changed to compatible types on overrides, but why does this limitation exist, either in the C# compiler or in the CLR? As I an see, there is nothing that could break if co/contra-variance was allowed, so what is the reasoning behind it? A similar question could be asked for widening access parameters - eg overriding a protected internal method with a public

Module initializers in C#

Deadly 提交于 2019-11-26 17:50:29
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 (e.g. CompilerGenerated, SpecialName) or other trickery that could be used? Do the C# / VB.NET ever

How to see JIT-Compilated code in .NET VM (CLR)

情到浓时终转凉″ 提交于 2019-11-26 17:40:41
问题 How can I have a trace of native code generated by the JIT-Compiler ? Thanks 回答1: In Visual Studio place a breakpoint in the code and start debugging. When it breaks, open the Disassembly window (Debug > Windows > Disassembly or Alt+Ctrl+D). 回答2: If you just use Debug->Windows->Disassembly on a standard Debug or Release exe, without modifying Visual Studio Debugging options, you will just see a version of non optimized .NET code. Have a look at this article "How to see the Assembly code

Is there a way to get the string representation of HRESULT value using win API?

▼魔方 西西 提交于 2019-11-26 17:36:07
问题 Is there a function in win API which can be used to extract the string representation of HRESULT value? The problem is that not all return values are documented in MSDN, for example ExecuteInDefaultAppDomain() function is not documented to return "0x80070002 - The system cannot find the file specified.", however, it does! Therefore, I was wondering whether there is a function to be used in common case. 回答1: You can use _com_error: _com_error err(hr); LPCTSTR errMsg = err.ErrorMessage(); If

Why C# is not allowing non-member functions like C++

五迷三道 提交于 2019-11-26 17:33:43
C# will not allow to write non-member functions and every method should be part of a class. I was thinking this as a restriction in all CLI languages. But I was wrong and I found that C++/CLI supports non-member functions. When it is compiled, compiler will make the method as member of some unnamed class. Here is what C++/CLI standard says, [Note: Non-member functions are treated by the CLI as members of some unnamed class; however, in C++/CLI source code, such functions cannot be qualified explicitly with that class name. end note] The encoding of non-member functions in metadata is

Retrieve JIT output

烂漫一生 提交于 2019-11-26 17:02:55
问题 I'm interested in viewing the actual x86 assembly output by a C# program (not the CLR bytecode instructions). Is there a good way to do this? 回答1: You should use WinDbg with SOS/SOSEX, ensure that method you want to see x86 code for is JITted in method tables and then see actual unassembly with u command. Thus you would see actual code. As others mentioned here, with ngen you could see code that is not exactly matches actual JIT compilation result. With Visual Studio it is also possible