Date:

Share:

Private and Static Constructors in C#

Related Articles

This article describes private and static builders in C #. In general, the department has all its builders declared public. While declaring the class builders as public ensures that we can create its objects, sometimes we do not require it. In other words, if we have to create a class in such a way that it will not be possible to create it. So we can do that by making his builders private. However, we can still use the functionality of this class. In order to do this, we can create a method within the class that returns its instance. We must also declare this method static. Because now we can not get any instance of this class before this static method is performed.

When should we use private builders in the classroom?

In case, a class contains only static methods, we must make sure that its occurrences do not take place. In this case, we declare a builder regardless of access or explicitly declare the builder as private. Thus, we can prevent this class from becoming cases. Because a class that contains only static methods does not display specific behavior. Then we can avoid creating his objects.

Static builders

Basically, a static constructor initializes the static fields of a class.

Plans to Demonstrate Private and Static Builders in C #

The following plan demonstrates the use of private builders. Because the FactorialFunctions class contains only two static methods, it has a private constructor. Therefore, we can not create this instance. To call methods, we only need the class name and the point operator. Moreover, the class is named My class Contains a builder regardless of access. Also, grade A Contains a builder that has been explicitly declared private.

using System;
namespace PrivateConstructorDemo

    class Program
    
        static void Main(string[] args)
        
           // FactorialFunctions ob = new FactorialFunctions();
            Console.WriteLine("Factorial of 6 = "+FactorialFunctions.Factorial(6));
            Console.WriteLine("Factorial of 9 = " + FactorialFunctions.RecursiveFactorial(9));
       
        
    
    class FactorialFunctions
    
        FactorialFunctions()  
        public static int Factorial(int v)
        
            int fact = 1;
            if (v == 0) return 1;
            for (int i = 1; i <= v; i++)
            
                fact = fact * i;
            
            return fact;
        

        public static int RecursiveFactorial(int v)
        
            if (v == 0)
                return 1;
            else
                return v * RecursiveFactorial(v - 1);
        
    


    class MyClass
    
        MyClass()  
    
    class A
    
        private A()  
        static void myfunction()
         
    

Productivity

Using a private constructor in the classroom

The following plan demonstrates the use of a static constructor. When using a static constructor we need to keep in mind certain important points about it. First, when the program is run, the static constructor runs first. Also, the static constructor operates only once. Moreover, a static builder can not take any argument. Therefore it can not be loaded on it. Also, he should not use any sub-access and cannot inherit.

using System;
namespace StaticConstructorDemo

    class Program
    
        static void Main(string[] args)
        
            Resident ob = new Resident();
            Console.WriteLine(ob);
            Resident ob1 = new Resident();
            Console.WriteLine(ob1);
            Resident ob2 = new Resident("Vikas", 20);
            Console.WriteLine(ob2);
        
    
    class Resident
    
        static int societyID;
        string name;
        int age;
        static Resident()
        
            societyID = 100;
            Console.WriteLine("Inside Static Constructor");
        
        public Resident()
        
            name = "Resident"; age = 1;
            Console.WriteLine("Inside Default Constructor");
        
        public Resident(string name, int age)
        
            this.name = name;
            this.age = age;
            Console.WriteLine("Inside Parameterized Constructor");
        
        public override string ToString()
        
            String str = "Society ID: " + societyID + " Resident Name: " + name + " Age: " + age;
            return str;
        
    
    class user
    
        static string society_email;
        string login_time;
        static user()
        
            society_email = "society@gmail.com";
        
        public user()
        
            login_time = DateTime.Now.ToString();
        
    
    class MyClass
    
        static int a;
        static MyClass()  a = 10; 
        public MyClass()  
    

Productivity

Using a Static Constructor in the classroom
Using a Static Constructor in the classroom

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles