Mike old cat comes from: the ideal of the old cat
This tutorial references to the C # and the ASP.NET programming tutorial, what is wrong, please point out, or in the ideal blog of the old cat. This time a brief summary operator
Arithmetic operator
The arithmetic operator includes plus ( ), subtract (-), multiplied (*), except (/), and for removing (%). In addition to the addition or decrease in integers and real numbers, the addition or subtraction operator is also suitable for enumeration type, string type, and delegate type, which is achieved by operators.
String mf1 = "mike";
String mf2 = "cat";
String mf3 = mf1 mf2; // mf3 = "mikecat"
Using system;
ENUM Weekday
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
Class Mikecat
{
Static void main ()
{
Weekday mf1 = weekday.sunday;
Weekday MF2 = MF1 3;
Console.writeLine ("MF1 = {0}, MF2 = {1}", MF1, MF2);
}
} // Results: Mf1 = Sunday, MF2 = Wednesday
2. Assignment operator
Assignment is given a new value for a variable. C # middle points simple assignment and composite assignment.
Simple assignment: "=" A = b = c equivalent to a = (b = c)
Composite assignment: " =" "- =" * = "" / = "" "=" "^ =" << = ">> =" A = 10 equivalent A = a 10
3. Compare operator
The comparison operator is used to compare the size of the two expressions, such as greater than (>) <==! = <=> =.
4. Logic operator
Logic and (&&) logic or (||) and logic are not (!)
5. The bit operator is an operator that calculates the data according to the binary position. C # bit operators include bits and (&) | ~ << >>
Using system;
Class Mikecat
{
Public static void main ()
{
INT A = 6 & 3;
Console.writeLine ("a = {0}", a);
// 6 binary is the binary of 00000110, 3 is 00000011, the bit is equal to 00000010, ie 2
INT B = 6 | 3;
Console.writeline ("b = {0}", b);
// 6 binary is the binary of 00000110, 3 is 00000011, bit or after equal at 00000111, ie 7
INT C = ~ 6;
Console.writeline ("c = {0}", c);
// 6 binary is 00000110, after the reverse reverse, 11111001 is -7
INT D = 6 ^ 3;
Console.WriteLine ("D = {0}", D);
// 6 binary is the binary of 00000110, 3 is 00000011, according to or equal to 00000101, that is, 5
INT E = 6 << 3;
Console.writeline ("E = {0}", E);
// 6 binary is 00000110, left-shifting three, equal to 00101000, ie 48INT f = 6 >> 2;
Console.writeLine ("f = {0}", f);
// 6 binary is 00000110, the right shift is equal to 0000001, ie 1
}
}
6.is operator
The IS operator is used to check if the runtime object type is compatible with a given type. E is an expression in the expression "E IS T", T is a type. The return value is a Boolean value.
If the following two conditions are met, the IS expression is calculated as true value:
Expression is non-NULL.
Expression can be converted to Type. That is to say, the conversion expression of the form (type) (type) (expression) is completed without trigger an exception.
Example
// cs_keyword_is.cs
// the is operator
Using system;
Class class1
{
}
Class class2
{
}
Public Class Istest
{
Public Static Void Test (Object O)
{
Class1 a;
Class2 b;
IF (o is class1)
{
Console.writeline ("o is class1");
A = (class1) O;
// Do Something with a
}
ELSE IF (o is class2)
{
Console.WriteLine ("o is class2");
B = (Class2) O;
// Do Something with B
}
Else
{
Console.Writeline ("o is neither class1 nor class2.");
}
}
Public static void main ()
{
Class1 C1 = New class1 ();
Class2 C2 = New Class2 ();
TEST (C1);
TEST (C2);
TEST ("a string");
}
}
Output
o IS class1
o IS class2
o Is neither class1 nor class2.
7. Trimer operator
Trimed operators (? :) also known as conditional operators. For conditional expression "B? X: Y", the condition B is always calculated first, and then the judgment is performed. If the value of B is True, the value of the X is calculated, otherwise the value of Y is calculated. The conditional operator is the right operation operator, so the form of expression a? B: c? D: e calculates as follows: a? B: (c? D: e)
Operator
Point operators are used for members access. Name1. Name2
Class SIMPLE
{
Public int A;
Public void b ()
{
}
}
Simple s = new simple ();
The variable s has two members a and b; if you want to access these two members, please use the point operator
S.a = 6; // Assign to Field A;
S.B (); // invoke member function b;
9. [] Operator
Square brackets ([]) are used for arrays, indexers, and attributes, can also be used for pointers.
Type [] array [indexexpr]
Where: Type type. Array array. Indexexpr Index Expression
10. () operator
In addition to specifying the order in which the operator is specified, the parentheses is also used to specify the conversion (type conversion)
(TYPE) EXPR
Where: TYPE
EXPR To convert the type name. EXPR an expression. Conversion Explicitly calls the converter from the EXPR type to the Type type; if such a conversion operator is not defined, the conversion will fail.
12. Self-increased self-reduction
The value of the self-increasing operator to the variable is 1, while the self-reduction operator - the value of the variable is reduced by 1. This operator has a prefixed point. For the prefix operator, the principle followed is "first increase or decrease, then use", and the suffix operator is just the opposite, "first use, then increased"
Using system;
Class Mikecat
{
Public static void main ()
{
Double X, Y;
X = 1.5;
Console.WriteLine ( x); // equal to 2.5 afterwards
y = 1.5;
Console.WriteLine (y ); // First display 1.5
Console.writeline (Y); // equal to 2.5 afterwards
}
}
13.AS operator
The AS operator is used to perform a conversion between compatible types. The AS operator is used in the following form: Expression As Type
Where: Expression
Reference type of expression. Type
Quote type.
The AS operator is similar to type conversion. When the conversion fails, the AS operator will generate empty, not an exception. In the form, this form of expression:
Expression as Type
equal to:
Expression IS TYPE? (TYPE) Expression: (TYPE) NULL
Only Expression is only calculated once.
Note that the AS operator only performs reference conversion and packing conversion. The AS operator cannot perform other conversions, such as user-defined conversions, such conversions should use the CAST expression instead of it.
Using system;
Class myclass1
{
}
Class myclass2
{
}
Public Class Istest
{
Public static void main ()
{
Object [] myObjects = new object [6];
MyObjects [0] = new myclass1 ();
MYOBJECTS [1] = new myclass2 ();
MyObjects [2] = "Hello";
MYObjects [3] = 123;
MYObjects [4] = 123.4;
MyObjects [5] = NULL;
For (int i = 0; i
{
String s = myObjects [i] as string;
Console.write ("{0}:", i);
IF (s! = null)
Console.writeline ("'" s "");
Else
Console.writeline ("Not a string");
}
}
}
Output
0: NOT A STRING
1: NOT A STRING
2: 'Hello'
3: NOT A STRING
4: NOT A STRING
5: NOT A STRING
14.new operator
New operator is used to create a new type of instance, there are three forms:
A: Object Create an expression for creating an instance of a class type or value type.
B: Array Create an expression for creating an array type instance.
C: Delegate to create an expression for creating a new entrustment type instance.
15.TypeOf operator
The TypeOf operator is used to obtain the type of the system prototype object.
Using system;
Class Mikecat
{
Public static void main ()
{
Console.writeLine (TypeOf (int));
Console.writeline (TypeOf (System.Int32));
}
} // Result: System.Int32 System.Int32
// Indicates that int and system.int32 is the same type
C # is used to get an expression at runtime with a getType () method.
Using system;
Class Mikecat
{
Public static void main ()
{
INT r = 3;
Console.writeline ("The area is equal to {0}", r * r * math.pi);
Console.writeline ("Type is {0}", (r * r * math.pi) .gettype ());
}
} // The area is equal to 28.274338823081
// Type is system.double
16.SizeOf operator
SizeOf operator gets a value type byte size
Using system;
Class Mikecat
{
Unsafe public static void sizesof ()
{
Console.Writeline ("SHORT size is {0}", SIZEOF (SHORT));
Console.WriteLine ("INT size is {0}", sizeof (int));
Console.writeline ("LONG is {0}", SIZEOF (long);
}
Public static void main ()
{
SIZESOF ();
}
} // SHORT size is 2; the size of the int 4; the size of the long is 8;
17.Checked and Unchecked operators
It is possible to generate overflow when performing integer arithmetic operations or switches from one integer display to another.
Check that there are two ways of processing in this overflow C #:
First: Set the overflow check option when compiling (overflow check is disabled):
CSC / Checked Test.cs // This is introduced before
Second: Use the Checked and Unchecked operators to determine if an overflow check is performed. Even if the overflow check is disabled, it also causes an exception when calculating.
Using system;
Class Mikecat
{
Public static void main (string [] args)
{
Long factorial = 1;
Long Num = Int64.Parse (args [0]);
For (Long Cur = 1; Cur <= NUM; CUR )
{
Checked {factorial * = cur;}
}
Console.writeline ("{0} is {1}", NUM, FACTORIAL);
}
} // Test.exe 3 3's step is 6
The unchecked operator is opposite to the checked operator. Even if overflow, the code hosted by the unchecked operator does not cause an exception.
I am not talking more about each operator. It is mainly hand tired. Ha ha. Still similar to the priority of C . See MSDN in detail. Thank you for paying attention to this tutorial, welcome to visit the eldest cat's ideal blog.