C # Basics Get started two loop statements
- Same as the usage in C language.
- Continue: The code behind the end of this loop (continue) is no longer executed and goes into the next loop (usually with if).
Array
- One-dimensional array definition:
int[] intArray;
- One-dimensional array initialization: after it is defined, it must be initialized before it can be used
- Dynamic initialization: Data type [] array name =new data type [array length]{element 1, element 2 ...} Dynamic initialization allocates memory space for an array element with the new operator and assigns an initial value.
- In the case of a given initial value, the elements take the default values, the numeric initial value is 0, and the Boolean type is false.
- Static initialization: data type [] array name ={element 1, element 2 ...}
- Get array Length: array name. Length (); The resulting value is the number of the array.
Enumeration
- To enumerate the possible occurrences, the enum keyword can be used to declare the enumeration type, and its value is limited to the enumerated values.
- Syntax: Enum Enum type name {enumeration value 1, enumeration value 2 ...}, for example:
enum Season{spring,summer,autumn,winter} where Season is equivalent to Int,season s=season.summer;
- Internally, an enumeration is associated with an integer value for each element, and by default the number starts at 0, followed by the number increment 1,spring=0,summer=1 ..., you can also assign the value yourself, and allow multiple enumerations to specify the same data value.
Structural body
- is a custom data type that is equivalent to a composite container that can store multiple types of data.
- Declaration of the struct body
struct body name {access modifier struct type member name; access modifier struct type member name;} example 1:public struct personstruct{public char sex; public int age; public float money;} Example 2:public struct personstruct {public char sex; public int age; public float score; public string name; }//define struct public static void Main (string[] args) {int A; Personstruct Per1; Per1.name = "Zhangsan"; Per1.sex= ' man '; Console.WriteLine ("Name: {0}, Gender: {1}", Per1.name, Per1.sex); }
- Access modifiers: All types and type members have access levels to control whether they can be used in other code in the current assembly or in other assemblies
- Public: Any other code in the same assembly, or other assemblies that reference the assembly, can access the type or member, and access is unrestricted;
- Private: Only code in the same class or struct can access the type or member, and access is limited to the containing type;
- Protected: A type or member that can only be accessed by code in the same class or struct or in a derived class of this class;
- Internal: Any code in the same assembly can access the type or member, but the code in the other assembly is not available.
- Note: Classes and structs that are named directly in the namespace (that is, not nested in other classes and structs), access modifiers can only be public and internal, and if no modifier is specified, the default is internal.
Reference type
int vel=10;//值类型object obj=vel;//把数值类型转换为引用类型Console.WriteLine("obj=" + obj);
int val=100;object o=val;int d=(int)o;
String
- The particularity of string: The string type represents a sequence of characters (0 or more Unicode characters). String is the alias of a string in the. Net framework. Although string is a reference type, the equality operator (= = and!) is defined. =) is to compare the value of a string object, which makes testing of string equality more intuitive.
- + in string, it is used to concatenate two strings, not arithmetic operators.
- String Access:
string str = "zhangxiaohan"; char c = str[4]; Console.WriteLine(c);
- To test whether a string is empty, you can use the
bool d = string.IsNullOrEmpty(str); Console.WriteLine(d);
- String methods (Figure 4, 5)
- 测试字符串是否包含所指定的字符串
bool t = str.Contains("zhang"); Console.WriteLine(t);
- 测试所包含字符串的类型:
int n = str.IndexOf("z"); Console.WriteLine(n);打印出来n为0
- split:图6
string str2 = "zhang xiao han"; char[] c1 = { ‘ ‘ }; string[] strArray = str2.Split(c1); foreach (string stri in strArray) { Console.WriteLine(stri); }
- The Stringbuilder:string object is immutable, Each time one of the methods in the System.String class is to create a new string object in memory, it is necessary to allocate a new space for the object, and if multiple modifications are required, the cost of the string is very large, so the System.Text.StringBuilder can greatly improve the efficiency.
- Statement:
StringBuilder s1=new StringBuilder("new") ;
- Shiyong1stringbuilder.append to add information:
s1.Append("string");
C # Basic Primer II