I really wonder why you have only 43K subscribers ?!!🤷♂️ It should be at least 1 million 👍 Very informative channel and contents. Greetings from Iran.
I like this video, but little advice to you. Try to give a small explanation about the pattern and then start with an example that makes more sense to learners. 😊
Interesting case. 'bad' version: if (!c.Contains("job_id", "job1")) { c.Write("job_id", "job1"); Console.WriteLine("Big Operation"); } when set .net core 3.1 in LinqPad generate null reference exception for : { _registry[key] = value; } Same exeption is in Visual Studio. :) but just change for: { Console.WriteLine(_registry == null); _registry[key] = value; } And it's OK.
@@RawCoding Yes, in LinqPad works fine, but in Visual Studio throws: "System.InvalidOperationException: 'Operations that change non-concurrent collections must have exclusive access. " This exception seems OK, but for net 3.1 "null reference" in VS and LinqPad seems very strange - probably some bugs in .NET :).
Great elucidation! But a problem here is that private constructor of safethread singleton class called 3 times, however it should have been called once. Anyone has any idea? ( No difference in the code)
It runs on app start, so long creation processes will cause long startup time, I know it can sound silly but some apps are REALLY BIG. Additionally it’s hard to control the flow of construction as well as lack of being able to use async.
Thanks for the video!! It was great. Can we do this without static ctor and lock? Wouldn't be it thread-safe and the instance will be created only once when Create() is invoked? Correct me if I'm wrong. public class MemoryCache { private static readonly MemoryCache _instance = new MemoryCache(); private MemoryCache() {} public static MemoryCache Create() { return _instance; } }
Because instance is null and we need to instantiate it, if it’s null there’s nothing to lock. You can create an empty implement that will indicate it needs instantiation but at that point it’s too much work.
What about Lazy? public sealed class Singleton { private static readonly Lazy lazy = new Lazy(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }