C # operator tutorial

xiaoxiao2021-03-30  197

Mike Old Cat

From: the ideal of the eldest 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 of integers and real numbers, add-oriented operators is also applicable to enumeration types, string types, and commission types, which are 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;

So short, you have to open the publication, depressed ...

转载请注明原文地址:https://www.9cbs.com/read-130536.html

New Post(0)