0
What are the uses of structure in C# programming and also differentiate between class and structure with example?
What are the uses of structure in C# programming and also differentiate between class and structure with example?
struct struct_name
{
//member variable
//function
//operator
//events
//delegates
}
using System
public struct Coords
{
public double X { get; }
public double Y { get; }
public Coords(double x, double y)
{
this.X = x;
this.Y = y;
}
public override string ToString() => $'({X}, {Y})';
}
public class Program
{
public static void Main()
{
Coords coor=new Coords(12.5,23.0);
Console.WriteLine(coor.ToString());
}
}
(12.5, 23)
| Structure | Class |
| Structs are of value types in C#. | Classes are of reference types in C#. |
| value types are allocated on stack memory. | reference types are allocated on heap memory. |
| Allocation and de-allocation is cheaper in value type as compare to reference type. | Allocation of large reference type is cheaper than allocation of large value type. |
| Struct can create an instance, with or without new keyword. | Classes used new keyword for creating instances. |