C # namespace, interface, class, structure (struct)

Source: Internet
Author: User
Namespace)

The definition method is defined using the namespace keyword. The Code is as follows:

 

Code
Namespace CSharp
{

}
Namespace CSharp. Example
{

}

 

"CSharp", "CSharp. Example" is the namespace name. The camel naming method is used to uppercase the first letter and the first letter of a word.

Interface)

First look at the Code:

Code
Interface TestInterface
{
Void SetValue (string name, string value );
String GetValue (string name );
}

Use the interface keyword to define. TestInterface is the name of this interface. The naming rules are the same as above, and the camel naming method is also used.
Void SetValue (string name, string value); -- "void" indicates the return type of the method. void indicates that no return is made, and string indicates that the return type is string.
String name, string value is the parameter list, and the definition method is "type + variable name". For example, we can use string name or int v. There is no limit on the number of parameters.

There is no specific implementation of the methods in the interface. In other words, there is no specific code.

How to Understand interfaces? For example, if we need to create a money printing machine, I will first define some specifications and write them into a file called the money printing machine specification (TestInterface). In this manual, I have defined two specifications,
First, I only need to put the printing machine into paper and ink (corresponding to SetValue (string name, string value )).
Second, put the paper and ink in it, and I can get the money (corresponding to GetValue (string name )).
As for how to make money after putting it into paper and ink, this is not the case of this manual. It is the case of the manufacturer.

Class)

 

Code
Public class TestPublicClass
{

Public int _ pi = 0;
Protected int _ pr = 0;
Private int _ pv = 0;
Internal int _ pn = 0;

Public int Pi
{
Set {_ pi = value ;}
Get {return _ pi ;}
}

Public string GetP (int I)
{

}

Protected void GetP2 ()
{

}

Private int GetP3 ()
{

}

Internal void GetP4 ()
{

}
}

 

Class definition method: public class TestPublicClass -- public is the access restriction character. Class is the keyword, and TestPublicClass is the class name. The naming rules are the same as above.

Detailed description of access limiters: http://msdn.microsoft.com/zh-cn/library/ba0a1yw2 (VS.80). aspx

Field definition: private int _ pv = 0; private is the access restriction character. Int is the type of this field, and _ pv is the field name. The naming rules are slightly different. In general, the field uses the camel naming method, but I personally like to add an underline in front of it, the first letter is lowercase, And the other words and letters are larger, for example, private int _ pValue;

Note: It is best to define all fields as private access levels. Public, protected, and internal are used for demonstration.

Field official reference: http://msdn.microsoft.com/zh-cn/library/ms173118 (VS.80). aspx

Attribute definition:
Public int Pi
{
Set {_ pi = value ;}
Get {return _ pi ;}
}

The format is not much explained, official reference: http://msdn.microsoft.com/zh-cn/library/x9fsa0sw (VS.80). aspx
The attribute naming rule is camel. The first letter is capitalized and the first letter of each word is capitalized, for example, PiValue. Attribute generally corresponds to a field.

Method definition:

Public string GetP (int I)
{

}

The rule is the same as the property, but the method provides more functions, official reference: http://msdn.microsoft.com/zh-cn/library/ms173114 (VS.80). aspx

There are examples in the official connection. just copy the test.

Structure (struct)

Official Address: http://msdn.microsoft.com/zh-cn/library/saxz13w4 (VS.80). aspx

Use Structure: http://msdn.microsoft.com/zh-cn/library/0taef578 (VS.80). aspx

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.