. Net Video Learning 2nd-quarter C # Object-oriented
Object-Oriented Inheritance 1
Find the namespace shortcut key for a class: ALT+SHIFT+F10
There is a class X in Project A, to use X in Project B, first add Project A to the reference in Project B, and then add using a in the Code of Project B, assuming that the namespace of Class A x is a, or the a.x format when using X.
Record run time
New Stopwatch (); SW. Start (); // Code SW. Stop (); Console.WriteLine (SW. Elapsed);
Start () and stop () are methods of class stopwatch, and elapsed are properties of class stopwatch.
The data of the value type is stored in the stack, and the data of the reference type is stored in the heap.
int Ten ; string " 123 ";
My own understanding is that a maps to an address, pointing to a location in the stack, which holds data 10, and S is also mapped to an address, pointing to a location in the stack, which holds another memory address that points to a location in the heap that holds data "123".
int Ten ; string " 123 " "456";
The immutability of a string: A is a value type, after a re-assignment of a value, the data in the stack 10 will be overwritten by the new data 20, S is a reference type, after the value of S is re-assigned, the data in the heap "123" is not destroyed, but in the heap of additional space to store the new data "456", 123 "is replaced by the address that points to" 456 ". Therefore, the data in the stack is overwritten. Repeating a string assignment produces a large number of data that is not pointed to in the heap.
string " 123 " ; string " 123 ";
To the top of the code, the intuitive feeling is that there are two blocks of space in the heap to save 2 "123", and then S1 in the stack is to point to the first "123" in the heap address, and S2 is a second "123" address. But the fact is that there is only one "123" in the heap, and S1 and S2 store the same address content in the stack.
String can be treated as a read-only char array, so it can be accessed using an index, but cannot be modified (read-only property). Note that this is only an understanding convenience, in fact the string is not a char array, but the string class inside provides a way to access the element through an index.
Of course, the string class does provide a way to convert to a char array:
string " 123 " ; Char [] CHS = S.tochararray ();
Instead of converting a char array to string, you can use one of the constructor methods of the String class:
string " 123 " ; Char [] CHS = S.tochararray (); chs[0'0'; s New string (CHS);
At this time s= "023", note that the heap still exists in the data "123", but the stack is not stored in the "123" address, but to the "023" address.
New StringBuilder (); string s = sb. ToString ();
The Stringbuiler class has a method Sb.append (x), which is more efficient than a string that re-opens the memory space.
Some properties and methods of the String class:
string " ABC " ;
string s2 = "abc";
Gets the string length by property length int sl = S1. Length;
String comparisons (C # operator overloading, support for string comparisons)
if (S1 = = s2)
{
Console.WriteLine ("S1=s2");
}
Else
{
Console.WriteLine ("S1!=s2");
}
String Case Conversion
String s3 = S1. ToUpper ();
String S4 = S1. ToLower ();
Similarly, a string comparison, the Equals () method can provide parameters to control the comparison method, the following comparisons are S1 and S2, while ignoring case
if (S1. Equals (S2, stringcomparison.ordinalignorecase))
{
Console.WriteLine ("S1=s2");
}
String splitting, Split () method (Overload 6): The first parameter is a char array, the character to remove and as a split, the second argument stringsplitoptions, the enumeration type, none will replace each delimiter with NULL, Removeemptyentries then removes the delimiter directly, returning a string array.
string " 1 2 + 345, 6 " ; Char [] CHS = {'+ ' +','}; string [] sa = S.split (chs,stringsplitoptions.removeemptyentries);
The above code implements the space in S, plus and comma minus, and returns a string array, a total of 4 elements, namely "1", "2", "345" and "6".
. NET Learning 2nd season C # Object-oriented inheritance 1