Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:32:18+00:00 2026-06-18T15:32:18+00:00

I came across this answer by Marc Gravell on how to create an object

  • 0

I came across this answer by Marc Gravell on how to create an object without calling its constructor. Can someone confirm this will not circumvent even the complete and best implementation of singleton pattern(Reference implementations here. And why? I guess more specifically I am not clear on the inner workings of GetSafeUninitializedObject() in the context of a Class’s constructors (static, private etc)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:32:19+00:00Added an answer on June 18, 2026 at 3:32 pm

    Within the singleton pattern you have a static variable on your type that will be initialized by the type constructor.

    By calling GetSafeUninitializedObject you only avoid the instance constructor which will be called after the type constructor.

    Example:

    public sealed class Singleton
    {
        private static readonly Singleton instance = new Singleton();
        private static string _StaticMessage = "Type ctor;";
    
        private string _Message = "init; ";
    
        static Singleton()
        { }
    
        private Singleton()
        {
            _Message += "ctor; ";
        }
    
        public static Singleton Instance
        {
            get { return instance; }
        }
    
        public string Message { get { return _StaticMessage + _Message; } }
    }
    
    internal class Program
    {
        private static void Main(string[] args)
        {
            var singleton = Singleton.Instance;
            // writes "Type ctor;init; ctor;"
            Console.WriteLine(singleton.Message);
    
            var instance = (Singleton)System.Runtime.Serialization.FormatterServices
            .GetSafeUninitializedObject(typeof(Singleton));
    
            // writes "Type ctor;"
            Console.WriteLine(instance.Message);
        }
    }
    

    Update to clarify difference between type initializer and static ctor asked from IllidanS4

    This doesn’t really belong to the answer above, but to the question within the comments: And a answer simply doesn’t fit into a simple comment.

    @IllidanS4: The type initializer is just a shortcut for writing an implicit static constructor. If you create a class that contains both initialization methods and decompile the resulting assembly you can see only one static constructor (.cctor) that will initialize all the variables. The both assignments will be merged where the type initializer will be called first and the statements within the static constructor last.

    Take this sample class:

    internal static class C
    {
        public static readonly string ByTypeCtor;
        public static readonly string ByTypeInitializer = "From type init; ";
        public static string ByBoth = "From type init; ";
    
        static C()
        {
            ByTypeCtor = "From static ctor; ";
            ByBoth += "From static ctor";
        }
    }
    

    If you compile it and afterwards decompile it (e.g. by using ILSpy) you’ll get the following code back:

    internal static class C
    {
        public static readonly string ByTypeCtor;
        public static readonly string ByTypeInitializer;
        public static string ByBoth;
        static C()
        {
            C.ByTypeInitializer = "From type init; ";
            C.ByBoth = "From type init; ";
            C.ByTypeCtor = "From static ctor; ";
            C.ByBoth += "From static ctor";
        }
    }
    

    Due to this fact i normally never use to initialize a variable directly when it will be declared. Instead i always leave them uninitialized (like the ByTypeCtor variable) and make all initializations within the constructor. This simply avoids cluttering variable initialization to different positions within the class which improves maintainability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this post and found belisarius' answer interesting. Wondering whether he someone
I came across this answer where the poster suggested that the shorthand for if(typeof
In trying to answer this question for myself I came across this nugget, after
I was browsing SO and came across this comment on an answer: Why do
I came across this post in Stackoverflow. The first answer mentions something like A
I was trying to answer a regex question for someone and I came across
I came across this syntax in a codebase and I can't find any more
A while ago I came across this answer that introduced me to the obscure
In attempting to Answer this Question I came across this in the output of
I came across a comment this answer which states that a library project: cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.