Interface and class, structure, interface class, Structure

Source: Internet
Author: User

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;

 


}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.