« first day (4318 days earlier)      last day (613 days later) » 

 
3 hours later…
8:05 AM
[Squirrel in Training] GoOoOoOoOoOoOoOoOoOoOoOd Mornin' pleberinos!
\[**[Squirrel in Training](https://discord.gg/PNMq3pBSUe)**] Sad Thursday..
A new low for SO-c#-chat
 
why is C# weird?
public static class Bar
{
    public static bool IsInitialized = Run(() =>
    {
        Console.WriteLine("initialized");
        return true;
    });

    static Bar()
    {
        Console.WriteLine("initializer");
    }

    public static void Initialize()
    {
        Console.WriteLine("initializing");
    }
}
if I invoke Initialize(), first IsInitialized is assigned (calling the Run() method which simply runs the lambda, therefor printing "initialized")
after that, the static constructor (or initializer) is invoked, printing "initializer"
after that Initialize() is invoked, printing "initializing"
but if I remove the static constructor, the field is not initialized any more
 
looks fine, why?
 
I assume that the field still would have been initialized
 
you removing static constructor thats why the static field didnt get 'init'
 
or at least not have different behaviour
why is the presence of a static constructor relevant?
I thought the runtime always initialized static fields before it exposes the static members
such as methods
 
8:14 AM
[milleniumbug] Jon Skeet has an article on this csharpindepth.com/articles/BeforeFieldInit
 
hmm
conclusion: C#'s silly nano-optimization process is silly
 
public static class Bar {
    public static bool IsInitialized = ((Func<bool>) (
        () => {
        Console.WriteLine("initialized");
        return true;
        }
        ))();
    public static void Initialize()
    {
        Console.WriteLine("initializing");
    }
}
try this?
 
only prints "initializing"
basically, the presence of a static constructor disables the beforefieldinit optimization
and that optimization is the cause of the difference in the behaviour
 
8:30 AM
.net 2?
 
4.8
 
I guess older versions dont have the same behaviour
my tests were with .net 6
 
hmmm
 
yea, framework 4.8 also prints both for me
probably something .net core specific
 
public static void Initialize() {
    if (!IsInitialized) Console.WriteLine("initializing");
}
Will this bypass 'optimization'?
 
8:39 AM
not really
 
-0-
really?
 
the method will be exposed before the fields are initialized
but when invoked, it will initialize the field as the field is accessed
 
Im guess the compiler didnt init that field because you didnt use it anywhere
 
that doesnt matter
in case of tree shaking, sure, but not in the current setup
but I dont understand what is unclear to you
the compiler simply delays initialization of fields beyond invokes of methods if the static constructor is not defined
 
then you put your your init into constructor
maybe the compiler thought you creating the static constructor for initialization purpose, so didnt init all the fields, for optimization
 
8:50 AM
no
that makes no sense
 
isn't it connected with the dynamic nature of dll? If you won't call into a .dll it won't run static init?
 
true
but that is not the subject
in fact, even if the dll is loaded, but nothing of the class is used yet, the class is not loaded
the class loader will initialize the necessary parts when you first access the class
normally, the class loader would initialize all static fields
but since the introduction of beforefieldinit, it might delay that intialization
 
loll what if they depend each other?
 
if the static constructor is present, it wont use the beforefieldinit optimization though
what about if they depend on each other?
 
found, jon skeet again
 
9:04 AM
yea, I assume that just throws an error
oh...
it doesnt
it just returns null
the more I use C#, the more I feel like it is inferior to our lord and saviour JVM
 
[milleniumbug] I have the opposite experience
 
comparing JVM as runtime?
or just Java as language?
as language, I can see why
but I dont understand what reason you would have to favour CLR over JVM
 
 
1 hour later…
10:12 AM
[Captain Obvious] What reason do you have to favour JVM over CLR?
 
 
1 hour later…
11:18 AM
primarily just that everything works the same and it does not do weird optimizations that actually significantly change the behaviour of your code
also the libraries
many more libraries and often work a bit nicer
occasionally too many libraries though
where you no longer know which one to pick
actually, there is one change in the runtime that will significantly affect behaviour of code
iirc, it should be released in the next release
and I am not sure what they decided on as solution
to be honest, the change only occurs on code that was abusing certain code that shouldnt be used in that way
but it was still a common pattern
oh, and interop :)
languages targeting the JVM have the JVM interop, which still has no equal
 
12:17 PM
[Squirrel in Training] its the lionm
[Squirrel in Training] he stooped
[Squirrel in Training] [someitmes]
 
occasionally
... quite rarely
always worth to take a picture for the picture book tho
 
 
8 hours later…
9:30 PM
 

« first day (4318 days earlier)      last day (613 days later) »