-------------------
Modifier
-------------------
You must already know that public, private, protected these modifiers used in C . Here I will discuss some new modifiers introduced by C #.
READONLY
The readonly modifier is only used in the data members of the class. As prompted by this name, Readonly data members can only read only, they can only assign a value at a constructor or directly initialize operation. Readonly is different from the Const data member, and Const requires you to initialize in a statement, which is done. Look at the sample code below:
Class myclass
{
Const int constint = 100; // Direct initialization
Readonly int myint = 5; // Direct initialization
Readonly Int Myint2; // Translator Note: Only declaration, not initialization
Public myclass ()
{
Myint2 = 8; // Indirect
}
Public func ()
{
Myint = 7; // illegal operation (Translator Note: No value is not assigned)
Console.writeline (Myint2.toString ());
}
}
SeaD (seal)
The sealing is not allowed to inherit, and it is not derived. So you can use Sealed keywords for classes you don't want to be inherited.
Sealed Class Cannotbetheparent
{
INT A = 5;
}
Unsafe
You can use the unsafe modifier to define an unsafe context. In an insecure context, you can write unsafe code such as a C pointer. Look at the sample code below:
Public unsafe myfunction (int * pint, double * pdouble)
{
INT * PANOTHERINT = New Int;
* panotherint = 10;
Pint = panotherint;
...
* PDOUBLE = 8.9;
}
-------------------
Interface
-------------------
If you have a COM concept, you will know what I want to talk about. An interface is an abstract base class, which only contains function description, and the implementation of these functions is done by subclasses. C # You have to use the interface keyword to define a class like an interface. .NET is based on such an interface. C # You do not support C allowed by multiple inheritances (translator Note: A derived class can be derived from two or more parent class). But multi-inheritance can be obtained through an interface. That is to say that you can derive it from multiple interfaces.
Using system;
Interface mydrawing
{
Int Originx
{
Get;
SET;
}
Int Originy
{
Get;
SET;
}
Void Draw (Object Shape);
}
Class Shape: MyDrawing
{
Int Orix;
Int Oriy;
Public int Originx
{
Get {
Return Orix;
}
SET {
Orix = value;
}
}
Public int Originy
{
Get {
Return Oriy;
}
SET {
Oriy = Value;
}
}
Public void Draw (Object Shape)
{
... // Do Something
}
// Class's OWN METHOD
Public Void MoveShape (int newx, int newy)
{
.....
}
}
-------------------
Arrays (array)
-------------------
The array in C # is better than the performance of C . The array is assigned in the heap and is therefore a reference type. You can't access elements that exceed a array boundary. Therefore, C # will prevent such a type of bug. Some auxiliary methods can cycle the function of accessing array elements in turn, and Foreach is such a statement. Compared with C , the characteristics of C # in array syntax are as follows:
Square brackets are placed after the data type instead of after the variable name.
Creating an array element To use a New operator.
C # supports one-dimensional, multi-dimensional and interleave arrays (arrays in arrays).
Example:
int [] array = new int [10]; // integer one-dimensional array
For (int i = 0; i Array [i] = i; Int [,] array2 = new int [5, 10]; // integer two-dimensional array Array2 [1, 2] = 5; Int [,] array3 = new int [5, 10, 5]; // integer three-dimensional array Array3 [0, 2, 4] = 9; int [] [] arrayofarray = = new int [2]; // Integer interleave array (array in arrays) ArrayofArray [0] = new int [4]; ArrayofArray [0] = new int in {1, 2, 15}; ------------------- Indexer ------------------- The indexer is used to write a method of accessing a set element, a collection of "[]", similar to the array. What you have to do is to list an index list of access instances or elements. The type of property belt is an input parameter, and the indexer is the index table of the element, in addition to this, the same grammar of both. Example: Note: CollectionBase is a library class for making a collection. List is a Protected CollectionBase member, stores a list of collections. Class Shapes: CollectionBase { Public Void Add (Shape SHP) { List.Add (SHP); } // Indexer Public Shape this [int index] { Get { Return (Shape) List [Index]; } SET { List [Index] = Value; } } }