Difference between struct
and class.
value
type and class is a reference
type.stack
and whereas class stores on the heap
.sealed
type to prevent class being inherited.// Base class
public class Parent
{
public void Print()
{
Console.WriteLine("Hello world!");
}
}
// Derived class
public class Customer : Parent
{
public int ID { get; set; }
public string Name { get; set; }
// class can have destructor
~Customer() { }
// class can have parameterless constructor
public Customer() { }
}
public class Program
{
static void Main(string[] args)
{
// int is vlaue type and stores in memory but get destroyed when its scope is lost.
// update 'j' will not affect i as they're two different copy in memory.
int i = 10;
int j = i
j = j + 1;
// Object reference variable
Customer C1 = new Customer
{
ID = 101,
Name = "Amelia"
};
// Both C1 & C2 pointing to the same memory location in heap.
Customer C2 = C1;
// Update its property will afect one another.
C2.Name = "Jaythan";
// output: C1 name: Jaythan, C2 anme: Jaythan
Console.WriteLine($"C1 name: {C1.Name}, C2 anme: {C2.Name}");
}
}