C # basic knowledge in ASP. NET,
C # basic knowledge in ASP. NET
(Asp.net, as a development framework, is now widely used. In addition to front-end html, css, JavaScript, and other backend most important language support, it is still C #, below we will summarize the basic knowledge that is mainly used, and learn more later.
I. C # is an object-oriented language used to develop applications that can run on the. net platform. Is a strongly typed language. Each variable must have a declaration type at a time. C # has two data types: Value Type and reference type. (The value type is used to store the value, and the reference type is used to store the reference of the actual data ).
1. Value Type
The value type indicates the actual data, which is stored in the stack. In C #, most basic types are numerical types. Value types include simple type, enumeration type, and structure type.
Simple types include numeric and bool types. (Generally, select the value type as needed. When the value is small, you can try to select the byte type ).
2. Reference Type
The reference type indicates the pointer or reference to the data, which can store the reference of the actual data. If the reference type is null, no object is referenced. Reference types include interfaces, classes, arrays, and pointers. The classes include the packing type, delegate, and custom class. (Note: Although string is an application type, if the equal operator = or! is used! = Is used to compare the value of the string object ).
3. packing and unpacking
Simply put, packing is a conversion from the value type to the reference type. In the same way, the case is converted from the reference type to the value type. You can use the unboxing function to operate complex reference types just like the simple type, which is also a powerful function of C.
Simple packing and unpacking instances
Class Test {static void Mian () {int I = 3; object a = I; // binning int j = (int) a; // binning }}
Note: During packing and unpacking, any value type can be considered as an object reference type. When a packing operation converts a value type to a reference type, it does not need to display forced type conversion; while the unpacking operation converts the reference type to the value type, since it can be forcibly converted to any compatible value type, it must be explicitly forced type conversion.
2. constants and variables
1. constants:It is also called a constant. It is known during compilation and remains unchanged during running. A constant is declared as a field. The const keyword is used before the field type during declaration. constants must be initialized at declaration. Constants can be labeledPublic, private, protected, internal, protected internalThese access modifiers define the way users access this constant.
2. Variables: The naming rules of variables must comply with the naming rules of the identifiers, and the variable names should be meaningful as much as possible for reading. Variables are the amount of constantly changing values during the running process. They are usually used to save the data input during the running process, intermediate results obtained by calculation, and final results.
You must declare a variable before using it. The variable can save a value of a given type. When declaring a variable, you also need to specify its name. Declaration Form :【Access modifier data type variable name ].
Modifier access level:
Public: allows members to access from any location
Protected: enables the Member to access from the inside of the declared class and its derived class
Private: A member accesses only from the class that declares it.
Internal: enables members to access only from within the declared assembly
Iii. type conversion
1. implicit type conversion
Implicit conversions are conversions that can be performed without declaration. During implicit conversion, the compiler can perform the conversion safely without checking.
Implicit type conversion table |
Source Type |
Target type |
Sbyte |
Short, int long double decimal |
Byte |
Short, ushort, int uint, ulong, float, double, decimal |
Short |
Int, long, float, double, decimal |
Ushort |
Int, uint, long, ulong, float, double, decimal |
Int |
Long float, double, decimal |
Uint |
Long ulong float double decimal |
Char |
Ushort int unit long float double decimal |
Float |
Double |
Ulong |
Float double decimal |
Long |
Float double decimal |
Note: loss of precision will occur when converting from int long ulong float to float.
2. Explicit type conversion
Explicit type conversion can also be called forced type conversion. It needs to declare the type to be converted in code. If there is no implicit conversion type, explicit type conversion is required.
You can use the Convert keyword to forcibly Convert data types.
Example: float f = 123.345;
Int I = (int) f;
Or: float f = 123.345
Int I = Convert. ToInt32 (f );
Note: Explicit conversions include all implicit conversions and Explicit conversions. A forced conversion expression can always be used to convert any numeric type to any other numeric type at a time.
Iv. Operators and expressions
C # provides a large number of operators that specify the operation symbols to be executed in the expression. An expression is a code snippet that can be calculated and the result is a single value, object, method, or namespace.
1. Arithmetic Operators and arithmetic expressions
Arithmetic Operators include +-*/and %. (Too simple to go into details );
2. Relational operators and relational expressions
Relational operators include: <>! ====<=>= (All languages are the same );
3. Value assignment operators and value assignment expressions
Assign operators are used to assign new values to variables, attributes, events, or index elements. Commonly used include: =, + =,-=, * =,/=, ^ =, % =, <= (right shift assignment), >=( left shift assignment).
4. logical operators and logical expressions
Logical operators include: & (and operator), ^ (XOR operator ),! (Non-operator), | (OR operator), which uses logical operators to connect the computing objects.
5. bitwise operators
Bitwise operators refer to their operands as a set of binary values. Each bitwise value can be 0 or 1. <Move left,> move right.
6. Other operators
Increment and decrease operators: ++, --, a --, a ++.
Conditional OPERATOR :? : Return one of the two values based on the value of a Boolean expression. For example: int a = 1; int B = 2;! = B? A ++: a --; (if! = B. The execution result returned by this instance is 2; otherwise, it is 1 ).
New OPERATOR: used to create objects and call constructors. For example, int I = new int (); equivalent to int I = 0;
As OPERATOR: used for conversion between compatible reference types. For example: string s = someObject as string; The as operator is similar to forced conversion. When the conversion fails, the operator produces null values instead of null values.
7. Operator priority
Basic> Single Object> multiplication and division> addition and subtraction> shift> comparison> equality> bitwise AND> bitwise OR> bit or> logic and> logic or> condition> value assignment
5. string processing
1. compare strings
The String class provides a series of methods for String comparison, such as CompareTo and Equals.
WhereCompareTo MethodUsed to compare whether two strings are equal. Format: String. CompareTo (String); Return Value Type
Equals MethodUsed to determine whether two string objects have the same value. Format: String. Equals (String); Return Boolean Type
2. Positioning and Its strings
Locate a character in a string or the position where zichuan first appearsIndexOf Method. Format: String. IndexOf (String); the parameter indicates the String to be located. (Case sensitive ).
3. format the string
. Net provides a flexible and comprehensive way to represent any numeric value, enumeration, date and time, and other basic data types as strings. Format is represented by a string with the format specifier. This string indicates how to represent the base type.
Format:String Format (String, Object );For example:
// Format it as the Currency type
String str1 = String. Format ("(C) Currency: {0: C} \ n",-123.4556f );
// Format it to the latest date type
String str2 = String. Format ("(d) Expiry date: {0: d} \ n", DateTime. Now );
4. truncate a string
SubString MethodYou can extract substrings from a specified string. Format: String. SubString (Int32, Int32); the first parameter indicates the starting position of the SubString, and the second parameter indicates the Ending position of the SubString.
5. Split the string
Split () methodA string can be split into a series of small strings according to a separator. Format: String [] Split (char []); the parameter is an array of Split strings.
String str = "hello world"; string [] split = str. Split (new Char [] {'.', '! '}); Foreach (string s in split) {if (s. Tirm ()! = ''') {Console. WriteLine (s);} // or change it to string [] split = str. Split (','.'! ');
6. insert and fill strings
Insert string:Insert () methodIs used to insert another string at the specified position of a string to construct a new string. Format: String. Insert (int, String); the first parameter is the specified Insert position.
Fill string:PadLeft ()Method andPadRight ()Method to add a specified number of spaces to achieve left-right alignment. Format: String PadLeft (int, char ). String PadRight (int Char );
7. Delete and cut strings
Delete string:Remove () methodDeletes a specified character at a specified position of a string. Format: String Remove (int, int); the first parameter indicates the location of the deletion, and the second parameter indicates the number of characters to delete.
Cut string: common methods used to cut the first redundant characters are:Trim (), TrimStart (), TrimEnd ();The format is as follows:
String Trim (Char []); // blank at the beginning and end of the String.
String TrimStart (Char []); // removes the character specified by the character String in the character array from the start of the String.
String TrimEnd (Char []); // removes the character specified in the character array from the end of the String.
8. Copy a string
Copy () methodYou can copy a string to another string. Format: String Copy (String); // The parameter is the String to be copied. The method returns the target String.
9. Replace string
Replace () methodIt can replace some specific characters or substrings in a string. Format: String Replace (string, string); the first parameter is the substring to be replaced, and the second parameter is the new substring after replacement.
Vi. Process Control
1. Branch statement
1> if... else statement
If (Boolean expression)
{Code snippet 1}
Else {code snippet 2}
2> switch statement
Switch)
{Case condition 1:
Break;
'''''
}
2. Loop statements
For () loop
While () Statement
Do while statement
Foreach statement
3. Exception Handling statement
Try... catch statement
Try... finally statement. The finally block is used to clear any resources allocated in the try block and run any code that must be executed even in the event of an exception. The control is always passed to the finally block, regardless of the exit mode of the try block.
Try... catch... finally statement
Throw statements are used to immediately and unconditionally trigger exceptions, and control statements that follow throw will never arrive.
VII. Array
An Array is a reference type derived from System. Array.
1. array declaration:
General Syntax: type [] arrayName; type [,] arrayName;
2. initialize data(There are many ways to initialize arrays. You can use the new operator to create array elements and initialize them to their default values)
// Example int [] arr = new int [6]; int [,] arr = new int [2, 3]; int [] arr1 = new int [3] {1, 2, 3}; int [,] arr2 = new int [3, 2] {2, 3}, {5}, {3, 5}; string [] arr; arr = new string [3] {"sd", "dddd", "aaaa"}; int [,] arr; arr = new int [,] {2, 3 }, {}, {}; // when creating an array, You can omit new and string [] arr = {"ddd", "fff", "sss "}; int [,] arr3 = {2, 3}, {4, 5}, {3, 2 }};
3. array Traversal
C # using the foreach statement to traverse the array is a simple and clear method to cyclically access the elements in the array.
int []arr={2,3,6,3,2};foreach(int i in arr){ system.Console.write({0},i);}
A simple asp.net development background with the above basic knowledge is part of the success and there is no end to learning.