C# Program to Demonstrate the Static Constructor in Structure Last Updated : 05 Dec, 2022 Comments Improve Suggest changes Like Article Like Report A Structure is a well-defined data structure that can hold elements of multiple data types. It is similar to a class because both are user-defined data types and both contain a bunch of different data types. You can also use pre-defined data types. However, sometimes the user might be in need to define its own data types which are also known as User-Defined Data Types. A structure can also hold constructors, constants, fields, methods, properties, and indexers, etc. We can create a structure by using the struct keyword followed by the name of the structure. Syntax: public struct structure_name { // Body of the structure } A Static constructor is a type of constructor that is called before the first instance of the structure or class gets created. It is initialized with static fields or data of the structure and is to be executed only once. We can create a static constructor by using static keyword and followed by constructor name. Syntax: static class() { // Body of the static constructor } Given a structure, now our task is to use a static constructor in the given structure. So to this, we have to follow the following approach. Approach Create a structure named GFG.Create a static constructor with no parameters inside the structure.public struct class { static class() { // Constructor body } }Create a non-static method named GFG by passing an integer parameter to it.Assign the variable to call the non static constructor in a separate method inside the structure.In the main method, create an object for the structure and access the both static and non static methods by creating the object.Example: C# // C# program to illustrate how to use the // static constructor in the structure using System; // Create a structure public struct GFG { // Static method names GFG static GFG() { Console.WriteLine("Hello! Static constructor is called"); } // Non static method public GFG(int variable) { Console.WriteLine("Hello! Non-Static constructor is called"); } } // Driver code class Geeks{ static void Main(string[] args) { // Create the object for // the structure GFG obj = new GFG(2); } } Output: Hello ! Static constructor is called Non-Static constructor is called Comment More infoAdvertise with us Next Article C# Program to Demonstrate the Static Constructor in Structure sravankumar_171fa07058 Follow Improve Article Tags : C# CSharp-programs Similar Reads C# Program to Demonstrate the Array of Structures An array of structures means each array index contains structure as a value. In this article, we will create the array of structure and access structure members using an array with a specific index. Syntax array[index].Structure(Details); where index specifies the particular position to be inserted 2 min read How to Define the Constructor Outside the Class in C++? A constructor is a special type of member function whose task is to initialize the objects of its class. It has no return type so can't use the return keyword and it is implicitly invoked when the object is created. Constructor is also used to solve the problem of initialization. It is called after 2 min read C# Program to Implement an Interface in a Structure Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. We can create a structure by using struct keyword. A structure can also hold co 2 min read C# | Difference between Static Constructors and Non-Static Constructors Prerequisite: Constructors in C# Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class. Below are the differences 6 min read How to access structure elements using Pointers in C# Unlike C/C++, Structures in C# can have members that are methods, fields, indexers, operator methods, properties or events. The members can have access specifiers as public, private, and internal. Pointers are variables that store the addresses of the same type of variable i.e. an int pointer can st 3 min read Private Constructors in C# Prerequisite: Constructors in C# Private Constructor is a special instance constructor present in C# language. Basically, private constructors are used in class that contains only static members. The private constructor is always declared by using a private keyword. Key PointsIt is the implementatio 3 min read Pointers to Structures in Objective C A pointer is a variable whose value is the address of another variable, e.g., stores the address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store variable addresses. It simplifies the programs and reduces their length. It is useful for 3 min read C# Program to Get the Count of Total Created Objects C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, we will create multip 3 min read How to Declare and Initialize an Array of Pointers to a Structure in C? Prerequisite: Structure in CArray in C In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers 8 min read âstatic constâ vs â#defineâ vs âenumâ In this article, we will be analyzing "static const", "#define" and "enum". These three are often confusing and choosing which one to use can sometimes be a difficult task. static const static const : "static const" is basically a combination of static(a storage specifier) and const(a type qualifier 4 min read Like