C # basic knowledge memo

Source: Internet
Author: User

Basic knowledge
I. Variables
Simple Integer type
Values allowed by aliases in type. Net frramework
Sbyte system. sbyte-128 ~ 127
Byte system. byte 0 ~ 255
Short sytetem. int16-32768 ~ 32767
Ushort system. uint16 0 ~ 65535
INT system. int32-2147483648 ~ 2147483647
Uint system. uint32 0 ~ 4294967295
Long system. int64-9223720306854775808 ~ 9223382036854775807
Ulong system. uint64 0 ~ 18446744073709551615
Simple floating point number
Type. Net frramework alias m e
Float System. Single 0 ~ 224-149 ~ 104
Double system. Double 0 ~ 253-1075 ~ 970
Decimal System. decimal 0 ~ 296-26 ~ 0
Other Types
Values allowed by aliases in type. Net frramework
Char system. Char: a Unicode character, which is stored as 0 ~ Integer between 65535
Bool system. boolean true or false
String sytem. String a group of strings
2. Constants
Constants have the following features:
A constant must be initialized during declaration. Once its value is specified, it cannot be changed.
The constant value must be used for computation during compilation. Constants cannot be initialized using the value extracted from a variable. To do so, read-only (readonly) should be used ).
A constant is static and cannot contain a modifier static in a constant declaration.
Iii. Value Type and reference type
The value type is stored in the stack, and the reference type is stored in the managed stack. C # The basic data types are all new. If you declare a bool variable and assign it the value of another bool variable, there will be two bool values in the memory. If the first bool variable is modified later, the value of the second bool variable does not change. These classes are copied by values.
Most of the Complex C # data types, including the classes we declare, are reference types. They are allocated to the heap, and their declaration cycle can span multiple function calls. It can be accessed by one or more aliases. CLR executes a fine-grained Algorithm To track which variables are still accessible and which referenced variables are no longer accessible. CLR regularly deletes inaccessible objects and returns the memory they occupy to the system, which is implemented through the garbage collector.
If you want to define your own type as a value type, you should declare it as a structure.
Iv. type conversion
There are two types of conversions:
Implicit conversions: conversions from type A to type B can be performed in all cases. The execution rules are very simple and can be performed by the compiler.
Display conversion: the conversion from type A to type B can only be performed in some cases. The conversion rules are complex and some type processing should be performed.
The implicit conversion rule is that any type of A can be implicitly converted to type B as long as its value range is completely within the value range of type B.
Using vonvert. To *** always performs overflow checks. The checked and unchecked keywords and project attribute settings are useless.
5. packing and unpacking:

Any value type and reference type can be converted to the object type. Boxing conversion refers to implicit or explicit conversion of a value type into an object type, or convert the value type to an interface-type applied to the value type ). To pack a value of the value type, create an object instance and copy the value to the object. The data in the packed object is in the heap, and the address in the heap is in the stack. The value of the boxed type is assigned to the object as a copy. For example:
Int I = 10;
Object OBJ = I; // implicit packing
Object OBJ = Object (I); // explicitly boxed
If (obj is int) // int
Console. writeline ("OK ");
Console. writeline (obj. GetType (); // system. int32
There are two ways to view the type of raw data packaged in the referenced object after packaging. To determine whether the original type is a given atomic type, use is. To return a string, use the GetType method of the object class.
Box-breaking conversion refers to explicitly converting an object type into a value type, or explicitly converting an interface type into a value type that executes this interface. Note that the packing operation can be performed implicitly, but the unpacking operation must be explicit. The unpacking process is divided into two steps: first, check the object instance to see if it is a boxed value of the given value type. Then, copy the Instance value to the value type variable. For example:
Int I = 10;
Object OBJ = I;
Int J = (INT) OBJ;
There are two ways to view the type of raw data packaged in the referenced object after packaging. To determine whether the original type is a given atomic type, use is. To return a string, use the GetType method of the object class.
Box-breaking conversion refers to explicitly converting an object type into a value type, or explicitly converting an interface type into a value type that executes this interface. Note that the packing operation can be performed implicitly, but the unpacking operation must be explicit. The unpacking process is divided into two steps: first, check the object instance to see if it is a boxed value of the given value type. Then, copy the Instance value to the value type variable. For example:
Int I = 10;
Object OBJ = I;
Int J = (INT) OBJ;
6. String
The sting type can be viewed as a read-only array of char variables. However, it cannot assign values to all characters in the string.
String mystring = "a string ";
Char mychar = mystring [1];
Char [] mychars = mystring. tochararray ();
String misting2 = mystring. tolower (); // toupper (); the original string is not changed.
String misting2 = mystring. Trim (); //. trimstart ();. trimend ()
Char [] trimchars = {'', '1 '};
String misting2 = mystring. Trim (trimchars); // Delete the specified string. If no parameter is specified, delete the space.
String mystring = "a string ";
String mystring = mystring. padleft (10, '*'); // padright (10, '*') is added to one side of the string.
String mystring = "this is test !";
Char [] trimchars = {''};
String [] mywords = mystring. Spilt (trimchars); // The Delimiter is deleted when the Delimiter is used.

The string must be enclosed in double quotation marks to give the stock index to the string variable. Use \ "to transfer double quotation marks. You can use the escape sequence \ n for line breaks in the string.
Console. writeline ("{0} {1}", mystring1, mystring2 );
A string is actually a template for inserting variable content. Each curly braces in the string are placeholders that contain the content of each variable in the list. Each placeholder (or formatted string) is represented by an integer contained in curly brackets, starting with 0 and increasing by 1 each time.
String is a reference type. When a string is assigned to another string, two references to the same string in memory are obtained. however, if one of the strings is modified, a New String object will be created, and the other string will not be changed.
VII. Enumeration
Enmu orientation: byte
{
North = 1,
Sourth = 2,
East = 3;
West = 4
}
Orientation mydir = orientation. North; // mydir = North
Byte dirbyte = (byte) mydir; // 1
String dirstring = conver. tostring (mydir); // north; you can also convert. tostring, but not (string)
String mystring = "West ";
Orientation newdir = (orientation) enmu. parse (typeof (orientation), mystring)
You can use a value as the basis and specify the same value for multiple enumerations. if no value is assigned, an initial value is automatically obtained, starting from 1 greater than the last definite value.

8. namespace
Namespace mynamesapce
{
Using Nt = namespacetwo; // the alias uses NT: Class Name
}
9. Expressions
Use & | as much as possible to improve performance to a certain extent.
^ If only one bit in the same position in the operand is 1, the result is 1.
Switch (myinteger)
{
Case <1>:
Case <2>:
// Do something
}
In fact, multiple conditions are checked at a time. If any of these conditions is met, multiple conditions will be executed. Code .
Do
{} While ();
The do while statement while must be followed by a semicolon.
10. Array
Arrays must be initialized before access. Initialization has two meanings: size (space allocation) and content.
Int Myint = {1, 2, 3}; // specify both the size and content
Int Myint = new int [3]; // The specified size is displayed. New will be assigned the default value.
Int Myint = new int [5] {1, 2, 3}; // The failure size must match the number of elements.
Int Myint = new int [arraysize]; // arraysize can be a variable
Int Myint = new int [arraysize] [1, 2, 3, 4]; // arraysize must be a constant
Double [,] Myint = new double [3, 4]; // initialize a multi-dimensional array
Double [,] Myint = {1, 2}, {1, 2, 3}; // initialize a multi-dimensional array
Int [] [] jaggeinarray = new int [3] [];
Jargeinarray = {1, 2, 3}, {1, 3}, {, 3} // Initialization is not allowed.
Jargeinarray [0] = new int [3];
Jaggeinarray [1] = new int [2]; // you can declare a subarray in the form of initialization.
Int [] [] jaggeinarray = {New int [] {1, 2, 3}, new int [] {1, 2} // You can also initialize it like this

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.