C # basic syntax Learning (1 ),
1. the C # constant data type can only be the original data type: int, bool, char, double, string, etc.
2. Access modifiers are used in C # To describe the accessibility of variables. The values can be private, protected, internal, protected internal, and public.
Public: Access is unrestricted and can be accessed anywhere.
Protected: access is limited to the current class and the derived class.
Internal: access is limited to the current Assembly.
Protected internal: access is limited to the current Assembly or classes derived from the current class.
Private: access is limited to the current class.
3. the variables declared in the method body cannot use access modifiers such as private and public.
4. The class member variables can be divided into two types: instance variables and static variables.
The instance variable belongs to the class instance, and each class instance has a backup of the instance variable. Instance variables of each class can be changed independently without affecting each other.
Static variables belong to the class. Each class and all its instances have only one backup of static variables. Changes to static variables anywhere will affect other instances.
5. Binary operators in C #: operand 1 ?? Operand 2
If operand 1 is not null, return the value of operand 1. Otherwise, return the value of operand 2.
6. enumeration in C # is a unique type composed of a group of naming constants. Each enumeration has a basic type, which can be any integer other than char. The default type of enumeration elements is int. By default, the first element value of the enumeration type is 0, and each element value increases by 1.
The enumeration syntax defined in C # Is: enum Enumeration type name [: basic type] {name constant 1 [= value] [, name constant 2 [= value]...}
Eg: enum Week {Sat, Sun, Mon, Tue, Wed, Thu, Fri };
Enum enumSample: long {e1 = 0, e2 = 10, e3 = 100 };
Console. writeLine ("enum: {0}, {1}, {2}", (long) enumSample. e1, (long) enumSample. e2, (long) enumSample. e3 );
7. Methods in C # can only be declared in the class. The declaration method is as follows:
[Modifier] return type method name ([[parameter modifier] parameter type 1 parameter name 1...])
{
// Method body
}
In addition to private, public, protected, internal, and static used to modify member variables, modifiers also include override and new used for method overloading.
Parameter modifiers can be none, ref, and out, indicating passing by value, passing by reference, and output parameters respectively.
When passing by value, a backup of the parameter value is passed. Therefore, this parameter has a backup in the method body and in the method body. The two are independent of each other, modification to parameters in the method body does not affect the variables in the method body.
The parameter memory address (referred to as reference) is passed by reference. In this case, parameters in the method body and variables in the method body use the same memory area, therefore, when the parameters in the method body are modified, the variables in the method body are affected.
Passing parameters in the out mode is also a passed reference, but the out modifier contains more information than the ref modifier, that is, the out parameter is an output parameter and will be assigned a value in the method body, therefore, the value passed to the out parameter can be an uninitialized variable.
8. arrays in C # are of the System. Array type. Therefore, you can use the attributes and methods of the System. Array class for arrays. C # declaration array Syntax:
Element type [] array name; // One-dimensional array
Element type [,] array name; // two-dimensional array
Int [] nums;
String [] words;
Int [,] r2array;
Colleague initialization during declaration:
Int [] nums = new int [] {1, 2, 3 };
String [] words = new string [] {"123", "hello "};
Int [] nums = {1, 2, 3 };
String [] words = {"one", "two", "three "};
9. The C # foreach loop traverses objects in the set.
Foreach ([type] variable name in set)
{
// Loop body
}