About
In this code snippet, we’ll make a struct in C#.
A struct is like a lightweight class but unlike a class(reference type) it’s value type. This can give you better performance in some cases.
You can use structs as a grouping mechanism to make a small “object” that contains multiple members instead of having them scattered around. If your struct grows too big(has too many properties/methods) you should consider making a class instead.
Note: A struct can’t inherit from another struct.
Let’s see how to make a struct in the code below.
Code:
using System;
namespace Struct
{
class Program
{
static void Main(string[] args)
{
//A struct is kind of a "light" version of a class.
myStruct st = new myStruct();
st.myMethod();
Console.WriteLine(st.myProperty);
Console.ReadLine();
}
}
struct myStruct
{
public string myProperty { get; set; }
public void myMethod()
{
Console.WriteLine("From myMethod.");
}
}
}





