Struct Constructor in C#

Hi Programmers, welcome to new article of c#.net. this article i’ll write the program of struct constructor in c# console application. A struct keyword is used to create a structure. A structure can contains variables,methods,static constructor,
parameterized constructor,operators,indexer,events and property.default we cant use with struct constructor. Let’s see the codes.

Directly Test below codes in Editor.

using System;
namespace ConsoleApp2
{
    class Program 
    {
        struct Emp
        {
            public int empno;
            public string ename, addr;
            public Emp(int empno1,string ename1,string addr1)
            {
                empno = empno1;
                ename = ename1;
                addr = addr1;
            }
        }
        static void Main(string[] args)  {
            Emp e = new Emp(111, "Jacob", "Jason");
            Console.WriteLine("Empno :" + e.empno + " Name " + 
                                e.ename + " Address " + e.addr);
        }
    }
}

You can use Struct Static Constructor. see below codes.

Happy Coding…Thanks.

Post Author: adama