Some issues that C ++ programmers need to pay attention to when learning C # (1)
1) Interface)
In C #, the keyword interface is used to define the interface. Moreover, multiple inheritance cannot be used in C.
Interface ICAR // interface ICAR
{
Int speed // attribute speed
{
Get;
Set;
}
Void run (); // Interface Method
Void stop ();
}
Class mycar: ICAR // inheritance Interface
{
Int _ speed;
Public int speed
{
Get
{
Return _ speed;
}
Set
{
_ Speed = value;
}
}
Public void run ()
{
System. Console. writeline ("mycar run, at speed {0}", _ speed );
}
Public void stop ()
{
System. Console. writeline ("mycar stop .");
}
}
2) use Arrays
Arrays in C # are allocated in the heap, so they are of reference type. C # supports arrays of the following types: single dimensions, multi dimensions, and array of arrays ).
Int [] array = new int [10]; // single-dimen1_array of int
For (INT I = 0; I <array. length; I ++)
Array [I] = I;
Int [,] array2 = new int [5, 10]; // 2-dimen1_array of int
Array2 [1, 2] = 5;
Int [,] array3 = new int [5, 10, 5]; // 3-dimen1_array of int
Array3 [0, 2, 4] = 9;
Int [] [] arrayofarray = new int [2]; // array of int
Arrayofarray [0] = new int [4];
Arrayofarray [0] = new int [] {1, 2, 15 };
3) use the index (Indexer)
The index allows you to retrieve data like an array.
Class indextest
{
Private int [] _;
Public indextest ()
{
_ A = new int [10];
}
Public int this [int Index]
{
Get
{
Return _ A [Index];
}
Set
{
_ A [Index] = value;
}
}
}
4) passing General Parameters
Class passarg
{
Public void byvalue (int x) // By-Value
{
X = 777;
}
Public void byref (ref int X) // By-ref
{
X = 888;
}
Public void byout (Out int X) // out
{
X = 999;
}
}
5) passing array parameters
Use the Params keyword to pass array parameters.
Class arraypass
{
Public void func (Params int [] array)
{
Console. writeline ("number of elements {0}", array. Length );
}
}
6) is and
OBJ is class is used to check whether the object OBJ belongs to the class object or convertable.
OBJ as class is used to check whether the object OBJ belongs to the class object or convertable. If yes, convert the object to the class type.
Class ISAs
{
Public void work (Object OBJ)
{
If (obj is mycar)
{
System. Console. writeline ("obj is mycar object ");
ICAR Ic = OBJ as ICAR;
IC. speed = 999;
IC. Run ();
IC. Stop ();
}
Else
{
System. Console. writeline ("obj is not mycar object ");
}
}
}
7) foreach
Int [] arr = new int [10];
Foreach (int I in ARR)
Console. writeline ("{0}", I );
8) virtual and override
The override keyword is required for the subclass to overload the parent class virtual function.
Class baseclass
{
Public Virtual void speak ()
{
System. Console. writeline ("Hello, baseclass .");
}
}
Class son: baseclass
{
Public override void speak ()
{
System. Console. writeline ("hello, son .");
}
}
Class grandson: baseclass
{
Public override void speak ()
{
System. Console. writeline ("Hello, grandson .");
}
}
9) use the keyword new to hide the function implementation of the parent class
Class shape
{
Public Virtual void draw ()
{
Console. writeline ("shape. Draw ");
}
}
Class rectangle: Shape
{
Public new void draw ()
{
Console. writeline ("rectangle. Draw ");
}
}
Class square: rectangle
{
Public new void draw ()
{
Console. writeline ("square. Draw ");
}
}
10) Call functions in the parent class
Use the keyword base to call functions in the parent class.
Class father
{
Public void Hello ()
{
System. Console. writeline ("Hello! ");
}
}
Class child: Father
{
Public new void Hello ()
{
Base. Hello ();
}
}