Static class VS Private constructor

Today I read about a static class and a private constructor.

Static class We cannot create an instance in a static class. we cannot inherit a static class. Only one instance is created.

Private constructor. We cannot create an instance. We cannot inherit. (I do not know how many instances are being created.)

I created two console applications, that is, one for the static class, for the private constructor.

Static class code

enter image description here

I understood one object in the generated form, since the constructor is called once.

Private constructor code

enter image description here

Now I did not understand if any object was created or not.

I have two questions.

1. Static. , , Private Constructor, Static-, .

2. , ?

.

EDIT:

, . , . .

1. : , . , . , " , ?"

2. , ? 0, CLR. , .

+4
5

, static, , static, . , .

static , , .

private , testPrivateConstructor. , private.

:

Q1: , , static. private static, ( ).

Q2: private, , , .

+3

.

public static class MyClass

( ) , , , , .

private MyClass()
{
}
private static MyClass _Singleton;
public static MyClass Singleton
{
    get
    {
        if(_Singleton==null) _Singleton = new MyClass();
        return _Singleton
    }
}

}

- , , ,

static MyClass()
{
    //configure static variables on first us only
    b = //read value from file or other resource not avalable at compile time
    a = b.Lenth; //can't be be done in class body as b would not have been initialised yet
}
private static int a;
private static string b;

ergo, , , ,

,

EDIT:

public static class StaticClassExample
{
    public static void ClassFunction()
    {
        Console.WriteList("This is a class function")
    }
}
public static class InitialisedStaticClassExample
{
    static InitialisedStaticClassExample()
    {
        Console.WriteList("This class has been initialised")
    }
    public static void ClassFunction()
    {
        Console.WriteList("This is a class function")
    }
}
public class PrivateConstuctorClassExample
{
    static PrivateConstuctorClassExample()
    {
        Console.WriteList("This class has been initialised")
    }
    private PrivateConstuctorClassExample()
    {
        Console.WriteList("This class has been Instantiated")
    }
    public static void ClassFunction()
    {
        Console.WriteList("This is a class function");
        var instance = new PrivateConstuctorClassExample();
        instance.InstanceFunction();
    }
    public void InstanceFunction()
    {
        Console.WriteList("This is a instance function")
    }
}
+1
  • , . .
  • . , .

    .

        class Program
        {
            static void Main(string[] args)
            {
                OnlyOne.SetMyName("I m the only one."); //static constructor will be called first time when the class will be referenced.
                Console.WriteLine(OnlyOne.GetMyName());
    
                NoInstance.SetMyName("I have private constructor"); //No constructor will be called when the class will be referenced.
                Console.WriteLine(NoInstance.GetMyName());
    
                Console.Read();
            }
        }
    
        static class OnlyOne
        {
            static string name;
            /// <summary>
            /// This will be called first time when even the class will be referenced.
            /// </summary>
            static OnlyOne()
            {
                name = string.Empty;
                Console.WriteLine("Static constructor is called");
            }
    
            public static string GetMyName()
            {
                return name;
            }
    
            public static void SetMyName(string newName)
            {
                name = newName;
            }
        }
    
        public class NoInstance
        {
            static string name;
            private NoInstance()
            {
                name = string.Empty;
                Console.WriteLine("No instance private constructor is called");
            }
    
            public static string GetMyName()
            {
                return name;
            }
    
            public static void SetMyName(string newName)
            {
                name = newName;
            }
        }
    }
    
0

2: 0

1: , , , , , - - no .

- . , . , - .

0

. . this

1 , . .

2 . , . , .

. . ,

,

public class TestPrivateConstructor
{

private TestPrivateConstructor()
{
  Console.WriteLine("Instance is created, Private Constructor called");
}

static TestPrivateConstructor _instance;

public static TestPrivateConstructor GetInstance()
{
    if(_instance == null)
    {
       _instance = new TestPrivateConstructor();
    }
    return _instance;
}

public static void DisposeInstance()
{
   if(_instance !=null)
   {
      _instance.Dispose();
      _instance = null;
   }
}
public void TestMethod()
{
  Console.WriteLine("Test MEthod Called");
}
void Dispose()
{
 //Do something
}
}

. .

class Program
{
   public static void Main(string[] args)
   {
     //Private constructor
     TestPrivateConstructor.GetInstance()
   }
}

Using the above approach, you can control the construction and destruction of an object. If you still have questions, please feel free to ask.

0
source

All Articles