Bài 08: Các toán tử (Operators) trong C#

0

Có một số toán tử hỗn hợp quan trọng như sizeof, typeof và ? : được  C# cung cấp:

Operator Description Example
sizeof() Trả về kích cỡ của kiểu dữ liệu sizeof(int), returns 4.
typeof() Tả về kiểu của class typeof(StreamReader);
& Trả về địa chỉ của một biến &a; returns actual address of the variable.
* Trỏ đến một biến *a; creates pointer named ‘a’ to a variable.
? : Biểu thức điều kiện If Condition is true ? Then value X : Otherwise value Y
is Xác định một đới tượng có thuộc loại nào đó If( Ford is Car) // checks if Ford is an object of the Car class.
as Ép kiểu mà không gây ra exeption nếu ép kiểu thất bại. Object obj = new StringReader(“Hello”);

StringReader r = obj as StringReader;

Ví dụ: 

using System;

namespace OperatorsAppl 
{
	class Program   
	{
		static void Main(string[] args)
		{         
			/* example of sizeof operator */
			Console.WriteLine("The size of int is {0}", sizeof(int));
			Console.WriteLine("The size of short is {0}", sizeof(short));
			Console.WriteLine("The size of double is {0}", sizeof(double));

			/* example of ternary operator */
			int a, b;
			a = 10;
			b = (a == 1) ? 20 : 30;
			Console.WriteLine("Value of b is {0}", b);
			b = (a == 10) ? 20 : 30;
			Console.WriteLine("Value of b is {0}", b);
			Console.ReadLine();
		}
	}
}

Kết quả sau khi chạy chương trình trên:6

  • Độ ưu tiên của toán tử:

Độ ưu tiên xác định thứ tự thực hiện toán tử trong biểu thức trong một biểu thức. Điều này ảnh hưởng đến kết quả của một biểu thức. Một số toán tử có độ ưu tiên cao hơn so với những toán tử khác khác; Ví dụ, phép nhân có độ ưu tiên cao hơn so với phép cộng.

Ví dụ x = 7 + 3 * 2; :  x được gán 13, không phải 20 vì toán tử * có độ ưu tiên cao hơn +, vì vậy ưu tiên 3 *2 và sau đó 7 được thêm vào nó.

Bảng sau đây là thứ tự ưu tiên của các toán tử. Ở đây, phía trên cùng của bảng là các toán tử có độ ưu tiên cao nhất và giảm dần về cuối bảng. Trong một biểu thức, các toán tử có độ ưu tiên cao được thực hiện đầu tiên.

Category Operator Associativity
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

Ví dụ:

using System;

namespace OperatorsAppl
{
	class Program
	{
		static void Main(string[] args)
		{
			int a = 20;
			int b = 10;
			int c = 15;
			int d = 5;
			int e;
			e = (a + b) * c / d;     // ( 30 * 15 ) / 5
			Console.WriteLine("Value of (a + b) * c / d is : {0}", e);
			e = ((a + b) * c) / d;   // (30 * 15 ) / 5
			Console.WriteLine("Value of ((a + b) * c) / d is  : {0}", e);
			e = (a + b) * (c / d);   // (30) * (15/5)
			Console.WriteLine("Value of (a + b) * (c / d) is  : {0}", e);
			e = a + (b * c) / d;    //  20 + (150/5)
			Console.WriteLine("Value of a + (b * c) / d is  : {0}", e);
			Console.ReadLine();
		}
	}
}

Chúc các bạn thành công!