By KL
Structures in C#: -
====================
Example: -
==========
class TestStruct {
public static void Main() {
SimpleStruct st = new SimpleStruct(4);
System.Console.WriteLine(st.i);
st.aMethod();
}
}
struct SimpleStruct {
public int i;
public SimpleStruct(int j) {
i=j;
}
public void aMethod() {
System.Console.WriteLine("Inside aMethod");
}
}
O/P
4
Inside aMethod
Features of Structures: -
==========================
* A structure or struct can contain only fields, methods, Etc.,
* In struct we can't initialize the variable whereas in class it can be possible
* It must be initialized either through function or using object
* Fields can't be given initial values at that time of creation.
* struct can't have a constructor.
* struct can't support the concept of inheritance
* We can derive a structure from an interface
* We can't inherit from sealed class
* A structure could not inherit from a class, the reverse is also true
* You can't declare a struct as sealed, because by default it is sealed
* A structure declare as abstract can't be used as a base class, i.e.,
be used in a derivation
* No destructor for struct
* struct are created on the stack and die when we reach the closing brace