Interface and class, structure, interface class, Structure
Interface:
The interface cannot be instantiated, and the interface can inherit the interface
Methods In the interface are abstract and there is no method body;
Interfaces can contain events, indexers, methods, and attributes.
Interface IA
{
String color {set; get ;}
Void Add ();
String this [int, index] {set; get ;}
Event TestDelegate TestEvent
}
Class:
One class can implement multiple interfaces
Abstract classes and interfaces cannot be instantiated.
1. class can be instantiated and belongs to the reference type. It is allocated to the heap (first-in-first-out, attribute) of the memory, Struct is a value type, and allocated to the stack (first-in-first-out, address) of the memory.
2. When the value type is converted to the reference type, it is called packing; when the reference type is converted to the value type, it is called unpacking;
(The reference type is allocated to the heap. Therefore, if two objects direct to the same address, the modified value will change to the original value.) (if the value type is in the stack, the value assignment equals to assigning all values to the P2 stack space, only references are saved)
3. The structure member cannot assign an initial value, the class can, and the structure cannot be instantiated.
4. The structure cannot be inherited, but interfaces and classes can be implemented.
5. The structure can define the structure function with parameters and initialize the member fields used
Namespace Structure
{
Public struct Person
{
Public string _ name; // The preceding underline must be used to differentiate a field from a variable.
Public int _ age;
Public Gender _ Gender;
}
Public struct MyColor
{
Public int _ red;
Public int _ green;
Public int _ blue;
}
Public enum Gender
{
Male and female
}
Class Program
{
Static void Main (string [] args)
{
Person;
Person. _ name = "James ";
Person. _ age = 18;
Person. _ Gender = Gender. Male;
Person P;
P. _ name = "rhubarb ";
P. _ age = 12;
P. _ Gender = Gender. Female;
Console. writeLine ("My name is {0}. I am {1} years old. I am a {2} Child", Person. _ name, Person. _ age, Person. _ Gender );
Console. writeLine ("My name is {0}. I am {1} years old. I am a {2} child.", P. _ name, P. _ age, P. _ Gender );
Console. ReadKey ();
// MyColor mycolor;
}
}