This article mainly introduces C # Fundamentals in ASP. Have a certain reference value, follow the small series below to see it
Description: As a development framework is now widely used, the basis of its development in addition to the front-end HTML, CSS, JavaScript and other back-end of the most important language support or C #, the following will be the main use of the basic knowledge to do a summary, the back of the study.
C # is an object-oriented programming language that is used primarily to develop applications that can run on the. NET platform. is a strongly typed language, so each variable must have a claim type. There are two types of data in C #: Value types and reference types. (where value types are used to store values, and reference types are used to store references to actual data).
1. Value types
Value types represent actual data and are stored in the stack heap. Most of the basic types in C # are numeric types. Value types include simple types, enum types, and struct types.
Simple types contain numeric types and bool types. (In general, choose a numeric type based on your needs.) When the value is less than an hour, try selecting the byte type.
2. Reference types
A reference type represents a pointer or reference to the data that stores a reference to the actual data. A reference type of NULL indicates that no object is referenced. Reference types include interfaces, classes, arrays, pointers, and so on. The class contains a boxing type, a delegate, and a custom class. (Note: Although string is an application type, if you use the equality operator = = or!) = When the value of a string object is compared.
3. Crating and Unpacking
Simply put, boxing is a conversion from a value type to a reference type. Similarly, unpacking is a conversion from a reference type to a value type. Using unboxing can manipulate complex reference types like simple types, which is a powerful feature of C #.
Examples of simple boxing and unpacking
Class Test {static void Mian () { int i = 3; Object A = i;//boxing int j = (int) a;//Unpacking}}
Note: During boxing and unpacking, any value type can be viewed as an object reference type. When a boxing operation converts a value type to a reference type, it does not require a display to force the type conversion, while a unboxing operation converts a reference type to a value type, because it can be cast to any compatible value type, it must be shown to force the type conversion.
Second, constant and variable
1, constants: Also known as constants, known at compile time and run the constant amount of the process, the constants are declared as fields, declared when the type of the field using the Const keyword, the constant must be initialized at the time of declaration. Constants can be marked as public, private,protected,internal,protected internal these access modifiers define how the user accesses the constant.
2. Variables: The naming rules for variables must conform to the identified naming rules, and the variable names should be as meaningful as possible for reading. A variable is the amount of value that is constantly being transformed while the program is running, usually to hold the data entered during the program's run, calculate the intermediate results, and the final result.
A variable must be declared before it can be used to hold a value of a given type. When declaring a variable, you also need to make its name. Declares the form of a variable: "Access modifier data type variable name".
The access level of the modifier:
Public: Enables members to access from any location
Protected: Enables a member to access from within the class that declares it and its derived class
Private: is a member only accessed from within the class that declares it
Internal: Enables a member to be accessed only from within the Assembly that declares it
Iii. type Conversion
1. Implicit type conversion
Implicit type conversions are conversions that do not need to be declared. When implicit conversions are performed, the compiler does not need to check to be able to convert safely.
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 |
Description: A loss of precision occurs when converting a simple type from an int long ulong float to float.
2. Explicit type conversions
An explicit type conversion can also be called a coercion type conversion, which requires declaring the type to be converted in code. If you are converting between implicit conversion types, you need to use an explicit type conversion.
Coercion type conversions can use the CONVERT keyword for data type casts.
For example: float f=123.345;
int i= (int) F;
Or: Float f=123.345
int I=convert.toint32 (f);
Description: Because explicit type conversions include all implicit type conversions and explicit type conversions, you can always convert from any numeric type to any other numeric type using a forced type conversion expression at a time.
Iv. Operators and expressions
C # provides a number of operators that specify which operations symbols are executed in an expression. An expression is a snippet of code that can be evaluated and result in a single value, object, method, or namespace.
1. Arithmetic operators and arithmetic expressions
Arithmetic operators include +-*/and%. (Too simple not to repeat);
2. Relational operators and relational expressions
Relational operators include: <>!= = = <= >= (all languages are the same);
3. Assignment operators and Assignment expressions
The assignment operator is used to assign a new value to a variable, property, event, or index element. Commonly used are: =, +=,-=,*=,/=,^=,%=,<<= (right Shift Assignment), >>= (left shift assignment), etc.
4. Logical operators and Logical expressions
Logical operators include:& (with operator), ^ (XOR operator),! (non-operator), | (or operator), which connects the operands with logical operators.
5, bitwise operators
The bitwise operator refers to its operand as a binary collection, each bits can take a value of 0 or 1. << left shift,>> right.
6. Other operators
Increment, decrement operator: ++,--,a--, a++.
Conditional operator:? : Returns one of two values based on the value of a Boolean type expression. For example: int a=1; int b=2; a!=b?a++:a--;(If a! =b, the instance returns an execution result of 2, otherwise 1).
New operator: Used to create objects and invoke constructors. For example: int i=new int (); equivalent to int i=0;
As operator: Used to perform conversions between compatible reference types. For example: string s =someobject as String; The as operator is similar to casting, and when the conversion fails, the operator produces a null value instead of throwing a null value.
7. Precedence of Operators
Basic >> monocular >> multiplication >> Add/subtract >> shift >> compare >> equals >> bit and >> bit XOR >> or >> logic with > > logic or >> conditions >> assignment
Five, string processing
1. Comparing strings
The String class provides a series of methods for string comparisons, such as CompareTo and equals.
Where the CompareTo method is used to compare the equality of two strings. Format: String.CompareTo (String); return numeric type
The Equals method is used to determine whether two string objects have the same value. Format: String.Equals (String); return Boolean type
2. Positioning and its string
Position a character in a string or the first occurrence of the Purple river using the IndexOf method. Format: String.IndexOf (String), where the argument represents a string that needs to be positioned. (Note the case-writing).
3. Formatting strings
. NET provides a flexible and comprehensive way to represent any numeric, enumeration, DateTime, and other basic data types as strings. Formatting is represented by a string representing the format specifier, which indicates how the base type is represented.
Format is: String format (string,object); for example:
Formatted as Currency type string Str1=string.format ("(C) currency:{0:c}\n", -123.4556f);//formatted as ShortDate type string str2= String.Format ("(d) shortdate:{0:d}\n", DateTime.Now);
4. Intercepting strings
The substring method can intercept substrings from the specified string. Format: string.substring (INT32,INT32); The first parameter represents the starting position of the substring, and the second parameter represents the end position of the substring.
5. Split string
The split () method splits a string into a series of small strings according to a delimiter. Format: string []split (char[]); The argument 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 modify to string []split=str. Split (', '. '! ');
6. Inserting and populating strings
Insert string: Insert () method to construct a new string by inserting another string at the specified location of a string. Format: String.Insert (int,string); The first parameter is the position of the specified insertion.
Padding string: The PadLeft () method and the PadRight () method add a specified number of spaces to align left and right. Format: String PadLeft (Int,char). String padright (int Char);
7. Delete and cut strings
Delete string: The Remove () method is used to delete the specified character at the specified location of a string. Format: String remove (int, int), the first parameter represents the location of the deletion, and the second parameter represents the number of deleted characters.
Clipping string: The usual way to use extra characters in the first place is: Trim (), TrimStart (), TrimEnd (), format as follows:
String Trim (char[]);//A blank space from the beginning and end of a string. String TrimStart (char[]);//Removes the character specified in the character array from the beginning of the string. String TrimEnd (char[]);//Removes the character specified in the character array from the end of the string.
8. Copying strings
The copy () method can copy a string into another string. Format: string copy (String),//parameter is the string to copy, and the method returns the target string.
9. Replace string
The replace () method can replace some specific characters or substrings in a string. Format: String replace (string, string); The first argument is the substring to be replaced, and the second worker argument is the new substring after substitution.
Vi. Process Control
1. Branch statements
1>if. Else statement
if (Boolean expression)
{Code Snippet 1}
else{Code Snippet 2}
2>switch statements
Switch (conditional)
{Case Condition 1:
Break
'''''
}
2. Circular statements
for () loop
while () statement
Do While statement
foreach statement
3. Exception Handling Statements
Try.....catch statements
The try.....finally statement, the finally block, clears any resources allocated in the try block, and runs any code that must be executed even if an exception occurs. Control is always passed to the finally block, regardless of how the try block exits.
Try...catch. Finally statement
Throw statement, which is used to throw an exception immediately and unconditionally, and the control never reaches the statement immediately following the throw.
Seven, array
An array is a reference type that derives from System.Array.
1, the Declaration of the array:
General syntax: Type[]arrayname;type[,]arrayname;
2. Initialize Data (there are many ways to initialize arrays, and you can create array elements by using the new operator to initialize 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,5},{ 3,5}};string []arr;arr=new string[3]{"SD", "dddd", "AAAA"};int [,]arr;arr=new int[,]{{2,3},{4,5},{4,2}};// You can omit new and array lengths when creating arrays string[]arr={"ddd", "FFF", "SSS"};int [,]arr3={{2,3},{4,5},{3,2}};
3. Array traversal
C # iterates through an array with a foreach statement, which is a simple and straightforward way to iterate through the elements in an array.
int []arr={2,3,6,3,2};foreach (int i in arr) {System. Console.Write ({0},i);}
Master the basic knowledge of the simple ASP. NET development background part of the success of a part of learning.