Operators in C#
In C#, operators are symbols that perform operations on variables and values. They allow you to manipulate data in various ways, such as performing arithmetic calculations, making logical comparisons, and working with strings. Now have a quick look on the code.
namespace Program
{
class Program
{
public static void Main(string[] args)
{
int a = 1;
int b = 2;
int c = 4;
string name = null;
int result;
// Arithmetic Operator
Console.WriteLine("Arithmetic Operator");
Console.WriteLine("A = {0}, B = {1}, C = {2}",a,b,c);
Console.WriteLine("Addition of A and B is : " + (a+b));
Console.WriteLine("Subraction of C to B is : " + (c-b));
Console.WriteLine("Multiplication of C and B is : " + (c*a));
Console.WriteLine("Divison of C to B is : " + (c /b));
// Assignmnt Operator
Console.WriteLine("\nArithmetic Operator");
Console.WriteLine("A Equal to B : " + (a = b));
Console.WriteLine("Assign to null variable : " + (name ??= "Manoj"));
Console.WriteLine("Compound Assignment B += C : " + (b += c));
// Relational Operator
Console.WriteLine("\nRelational Operator");
Console.WriteLine("Equal to Operator a == b : " + (a == b) );
Console.WriteLine("Greater than Operator a > b: " + (a > b) );
Console.WriteLine("Less than Operator a < b: " + (a < b) );
Console.WriteLine("Greater than or Equal to a>=b: " + (a>=b) );
Console.WriteLine("Lesser than or Equal to a<=b: " + (a<=b) );
Console.WriteLine("Not Equal to Operator a != b: " + (a != b) );
// Bitwise Operator
Console.WriteLine("\nBitwise Operator");
result = a & b;
Console.WriteLine("Bitwise AND a & b: " + result);
result = a | b;
Console.WriteLine("Bitwise OR a | b: " + result);
result = a ^ b;
Console.WriteLine("Bitwise XOR a ^ b: " + result);
result = ~a;
Console.WriteLine("Bitwise Complement ~a: " + result);
result = a << 2;
Console.WriteLine("Bitwise Left Shift a << 2: " + result);
result = a >> 2;
Console.WriteLine("Bitwise Right Shift a >> 2: " + result);
// Conditional or Ternary Operator
Console.WriteLine("\nConditional Operator");
name = a > b ? "A is Greater " : "B is Greater or Equal";
Console.WriteLine("(a>b)? true : false Result: " + name);
bool x = true, y = false, res;
// Logical Operator
Console.WriteLine("\nLogical Operator");
res = x && y;
Console.WriteLine("AND Operator x && y : " + res);
res = x || y;
Console.WriteLine("OR Operator x || y: " + res);
res = !x;
Console.WriteLine("NOT Operator !x: " + res);
}
}
}
ย