1, Basic code structure:
1 using System; 2 namespace HelloWorldApplication 3 { 4 class HelloWorld 5 { 6 static void Main(string[] args) 7 { 8 /* My first C# program*/ 9 Console.WriteLine("Hello World"); 10 Console.ReadKey();
//This causes the program to wait for the action of a key to prevent the screen from running quickly and closing when the program starts from visual studio. Net. 11 } 12 } 13 }
c#'s file suffix:. cs
C# is case sensitive, and every sentence should be in Chinese; The end and file name can be different from the class name. The definitions of classes and variables are consistent with C + +
Console.ReadLine() Will wait until the user presses enter, reading in one line at a time.
Console.ReadKey() Is to wait for the user to press any key and read one character at a time.
Console.WriteLine("Area: {0}", GetArea()); // Print the result returned by the function
2, Data type
1 Value type( Value types) 2 Value types contain data directly. For example bool,double, int,char,float,They store Boolean values, double precision floating-point numbers, characters, floating-point numbers, respectively. 3 application sizeof(int) Look at the storage size, a few bytes 4 Decimal type: decimal
5 5 Reference type( Reference types) 6 Contains references to variables. They refer to a memory location. 7 Built in reference types are:object,dynamic and string. 8 Object( Object)type 9 System.Object Class. So the object( Object)A type can be assigned a value of any other type (value type, reference type, predefined type, or user-defined type). 10 However, type conversion is required before assigning values. 11 When a value type is converted to an object type, it is called boxing; on the other hand, when an object type is converted to a value type, it is called unpacking. 12 object obj; 13 obj = 100; 14 15 Dynamic( Dynamic)type 16 You can store values of any type in dynamic data type variables. Type checking of these variables occurs at run time. This is different from object types 17 dynamic <variable_name> = value; 18 dynamic d = 20; 19 20 String( String)type 21 System.String Class. It is from the object( Object)Type derived string( String)The value of type can be assigned in two forms: quotation marks and @Quotation marks. 22 @ You can wrap any line in the string. Line breaks and indented spaces are calculated within the length of the string 23 String str = "runoob.com"; 24 string str = @"<script type=""text/javascript""> 25 <!-- 26 --> 27 </script>"; 28 C# string The string can be preceded by @(call"verbatim string literal ")Escape character(\)Treat as normal characters 29 string str = @"C:\Windows"; 30 == 31 string str = "C:\\Windows"; 32 Pointer type( Pointer types) 33 to C++identical 34 char* cptr; 35 int* iptr;
3, Type conversion
- Implicit type conversion - These conversions are C# default conversions that are performed in a safe manner and do not result in data loss. For example, conversion from a small integer type to a large integer type, and conversion from a derived class to a base class.
- Explicit type conversion - Explicit type conversion, that is, cast type conversion. Explicit conversion requires cast operators, and cast will cause data loss.
Strong rotation double d = 5673.74; int i; // Force conversion double by int i = (int)d; Built in conversion method ToDateTime Converts a type (integer or string type) to a date-Time structure. ToInt64 Convert type to 64 Bit integer type. ToString Converts a type to a string type. int i = 75; Console.WriteLine(i.ToString());
4, C# variable
Enumeration type: enum enumeration name {enumeration element 1, enumeration element 2,...};
Definition of variables:
<data_type> <variable_name> = value;
variable_list can consist of one or more comma separated identifier names. It can be initialized directly
num = Convert.ToInt32(Console.ReadLine());
Accept user values: function Convert.ToInt32() Convert user input data to int data type because Console.ReadLine() Only data in string format is accepted.
5, C # constant
Integer constant:
Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the cardinality: 0X or 0X indicates hexadecimal, 0 indicates octal, and no prefix indicates decimal.
85 / * decimal*/ 0213 / * octal*/ 0x4b / * hex*/
Integer constants can also have suffixes, which can be a combination of U and L, where U and L represent unsigned and long respectively. Suffixes can be uppercase or lowercase, and multiple suffixes can be combined in any order.
30 /* int */ 30u / * unsigned int*/ 30l /* long */ 30ul / * unsigned long*/
Floating-Point Literals
Floating point constants are expressed in decimal or exponential form.
3.14159 / * legal*/ 314159E-5L / * legal*/
Must contain a decimal point, an exponent, or both. When expressed as an exponent, it must contain an integer part, a decimal part, or both. Signed exponents are represented by E or E.
character constants
\a | Alert or bell |
\b | Backspace |
\f | Page feed |
\n | Newline |
\r | enter |
\t | Horizontal tab |
string constant
string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me string f = @"Joe said ""Hello"" to me"; // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt string h = @"\\server\share\file.txt"; // \\server\share\file.txt
Constant definition
const <data_type> <constant_name> = value;
Five operators
- Arithmetic operator
+,-,*,/,%,++,--
c = a + +: assign a to c first, and then perform auto increment operation on A. c = ++a: first perform auto increment operation on a, and then assign a to c.
- Relational operator
==,!= , > , < , >= ,<=
- Logical operator
&&, |,! (and or not)
- Bitwise Operators
Perform operations bit by bit. &, |, and^
&: the same is the same, otherwise it is 0
|: one is one, otherwise it is 0
^: the same is one, the difference is 0
~: including sign bits, all negative
< <: binary shift left operator, followed by 0: a < < 2 will get 240, that is, 1111 0000
>>: binary shift right operator. If 0: a > > 2 is added before it, 15 will be obtained, that is 0000 1111
- Assignment Operators
=,+=,-=,*=,/=,%=,<<=, >>=, &=, ^=, |=
- Other Operators
&: take the address;