Answers to “Questions on .Net”

    In my POST post, I asked a few questions about .NET and clr and promised to answer them. I answer:

    1) When the constructor will not be called when creating the type instance?
    I know 2 cases: when deserializing an object and when copying an object using the MemberwiseClone method of an Object object

    2) How does the c # compiler interpret the static modifier before the class (in IL code)?
    in the IL code will be about the following items: . in addition, the compiler also will not create a constructor method in the class 3) Which access modifier for fields and methods does not implement c #, but implements IL code? family and assembly - available only to methods in the defining type (and types nested in it) and derived types in the defining assembly 4)
    class public abstract auto ansi sealed beforefieldinit





    Do you know how to declare Union in C # (imitate)? Those. make several fields start at a single offset in memory. 5) Have you ever wondered why for meaningful types (struct) you cannot define a constructor without parameters (in C #)? Is it possible to get around this? :) C # does not allow defining constructors without parameters for meaningful types, so as not to mislead developers about which constructor is called (the one that initializes everything with zeros, or ours) On IL, unlike c #, you can define such a constructor. (how it will work is another question) 6)
    [StructLayout(LayoutKind.Explicit)]
    internal struct SomeValType {
    [FieldOffset(O)] Byte b; // Поля b и х перекрываются
    [FieldOffset(O)] Int16 x; // в экземплярах этого класса.
    }






    If we supplied the client with a code where the fields were defined in the type, the client wrote our code on the basis of our code, then we delivered the client a new version of our code where the fields were replaced with properties, what could break in the client’s code?
    Of course, first of all, all methods will stop compiling, where the parameters were fields (and now properties) with out or ref ... In addition, the
    property can be read-only,
    the property method can lead to an exception,
    the property method can be executed for a long time,
    if When a property is called several times in a row, the property can return different values ​​(4Ex.:DateTime.Now);
    besides, if you used reflection, then there will also be an exception when binding.

    7)All event handlers should return void ... Does Microsoft follow its requirement in the FCL library? :))
    Indeed, such a requirement is embedded in the mechanism of events. This is mandatory because when an event occurs, several callback methods may work and it will be impossible to get the return value from them.
    Microsoft, as always, in its own way - its recommendations should not be - an example is the ResolveEventHandler event handler that returns an object of type Assembly. (proof link: tyts )

    PS Questions and answers prepared by CLR via C # by Jeffrey Richter ...

    Also popular now: