Structure in C# with Example

Structure in C# with Example
Structure in C# with Example

In C#, structure is very useful if you want to store some custom data.  Like a Class in C#, a structure can also have fields, methods, properties, and constructors.  Structure is pretty much similar to a class, but there are some differences between a structure and a class.  Structure is of value type and class is of reference type.  A very common example of structure is System.Int32 known by alias int and System.Double known by alias double, both of them are of value type.  To create structure, we use struct keyword followed by structure name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello
{
    //using structure
    struct Employee
    {

        //Auto-Implemented property
        public string Name { get; set; }      
        

        public void Print()
        {
            Console.WriteLine(Name);
        }

    }


    class Program
    {
        static void Main(string[] args)
        {

            Employee emp = new Employee();
            emp.Name = "Roberto Carlos";
            emp.Print();

        }

    }
}

 Output:

Roberto Carlos
Press any key to continue . . .