C# Operators
In C#, operators are fundamental building blocks that allow you to perform various operations on data, such as arithmetic calculations, comparisons, and logical evaluations. Let’s explore some of the essential operators in C# and understand how they work.
Arithmetic Operators:
Operator | Description | Example | Output |
+ | Addition | int result = 5 + 3; | 8 |
– | Subtraction | int result = 7 – 2; | 5 |
* | Multiplication | int result = 4 * 6; | 24 |
/ | Division | int result = 10 / 2; | 5 |
% | Modulus (Remainder) | int result = 10 % 3; // 3 divide by 10 so remainder is 1 | 1 |
++ | Increment by 1 | int a = 5; a++; | a becomes 6 |
— | Decrement by 1 | int b = 7; b–; | b becomes 6 |
Comparison Operators:
In cases where you want to define a variable i with a comparison operator then we will use them as follows
The comparison operator will give a condition and the output is true or false
Operator | Description | Example |
== | Equal to. If I =5 | (i == 5) |
!= | Not equal to, if i not equal to 2 | (i != 2); |
> | Greater than, if i is greater than 6 | (i > 6); |
< | Less than, if i is less than 2 | (i < 2); |
>= | If i is Greater than or equal to 2 | (i >= 2); |
<= | If i is Less than or equal to 3 | (i <= 3); |
Logical Operators:
The logical operator will give a condition and the output is true or false
Operator | Description | Example |
&& | Logical AND. If the person is equal to Male and the age is equal to 21 | person == male && age == 21 |
|| | Logical OR. If the person is male or female | person == male || person == female |
! | Logical NOT (Negation). If a person is not equal to a male | Person != male |
Conditional Operator:
Structure:
condition ? trueResult : falseResult;
Condition | Expression | Explanation | Example | Result |
True | condition ? trueResult : falseResult | If the condition is true, the result is trueResult. | int 5 > 3 ? “Yes” : “No” | “Yes” |
False | condition ? trueResult : falseResult | If the condition is false, the result is falseResult. | int 5 < 3 ? “Yes” : “No” | “No” |
Comparing integers | a > b ? “a is greater” : “b is greater” | Compares two integers and returns a message accordingly. | 10 > 20 ? “10 is greater” : “20 is greater” | “20 is greater” |
Checking null values | x != null ? x.ToString() : “null” | Checks if a variable is null and returns its string value or “null”. | string str = null; str != null ? str : “null” | “null” |
Even or odd number | num % 2 == 0 ? “Even” : “Odd” | Checks if a number is even or odd and returns the result. | 7 % 2 == 0 ? “Even” : “Odd” | “Odd” |
Age check | age >= 18 ? “Adult” : “Minor” | Determines if a person is an adult or a minor based on age. | int age = 20; age >= 18 ? “Adult” : “Minor” | “Adult” |
Assigning values | bool isTrue = true; int value = isTrue ? 1 : 0; | Assigns a value based on a Boolean condition. | bool isTrue = false; int value = isTrue ? 1 : 0; | 0 |
Default value | var result = input ?? “default”; | Uses the null-coalescing operator to provide a default value if input is null. | string input = null; string result = input ?? “default”; | “default” |
For Example :
using System;
class Program
{
public static void Maximum()
{
int marksofenglish = 10;
int marksofscience = 20;
int result = marksofenglish > marksofscience? marksofenglish : marksofscience;
Console.WriteLine(result);
}
static void Main()
{
Maximum();
}
}
Explanation:
using System;
class Program
{
public static void Maximum()
This defines a method named Maximum. The public keyword means this method can be called from outside the class. The static keyword means this function belongs to the class itself, not to any specific object of the class.
int marksofenglish = 10;
This declares an integer variable named marks1 and sets its value to 10.
int marksofscience = 20;
This declares another integer variable named marks2 and sets its value to 20.
int result = marksofenglish > marksofscience? marksofenglish : marksofscience;
This line uses a conditional (ternary) operator to find the larger of the two numbers. It checks if marksofenglish is greater than marksofscience. If true, result is set to marksofenglish. If false, the result is set to marksofscience. In this case, marksofscience is larger, so the result will be 20.
Console.WriteLine(result);
This line prints the value of the result to the console. It will print ” 20″.
static void Main()
This defines the Main method, which is the entry point of the program. When you run the program, this method is executed first.
Maximum();
This line calls the Maximum function, which then executes the code inside that function.
For example:
using System;
class Program
{
public static void AgeCheck()
{
int age = 20;
string status = age >= 18 ? “Adult” : “Minor”;
Console.WriteLine(status);
}
static void Main()
{
AgeCheck();
}
}
Explanation:
using System;
class Program
{
public static void AgeCheck()
This method checks if a person is an adult, or a minor based on age.
int age = 20;
This declares an integer variable named age and sets its value to 20.
string status = age >= 18 ? “Adult” : “Minor”;
This line uses a conditional (ternary) operator to determine if the person is an adult or a minor based on the age value. It checks if age is greater than or equal to 18. If true, the status is set to “Adult”. If false, the status is set to “Minor”. In this case, the age is 20, so the status will be “Adult”
Console.WriteLine(status);
Print the status to the console.
static void Main()
Main method – the entry point of the program.
AgeCheck();
Call the AgeCheck method.
For example:
using System;
class Program
{
public static void AssignValues()
{
bool privatecustomer = false;
int value = privatecustomer ? 1 : 0;
Console.WriteLine(value);
}
static void Main()
{
AssignValues();
}
}
Explanation:
using System;
class Program
{
public static void AssignValues()
{
bool private customer = false;
This declares a Boolean variable named privatecustomer and sets its value to false.
int value = privatecustomer ? 1 : 0;
This line uses a conditional (ternary) operator. It checks if privatecustomer is true or false. If true, the value is set to 1. If false, the value is set to 0. In this case, the privatecustomer is false, so the value will be 0
Console.WriteLine(value);
Prints value to console
static void Main()
{
AssignValues();
Call the AssignValues method to the main method
For example:
using System;
class Program
{
public static void AssignDefault()
{
string input = null;
string result = input ?? “default”;
Console.WriteLine(result);
}
static void Main()
{
AssignDefault();
}
}
Explanation:
using System;
class Program
{
public static void AssignDefault()
{
string input = null;
This declares a string variable named input and sets its value to null.
string result = input ?? “Hello World”;
It checks if the input is not null, the result gets the same value as the input value. If the input is null, the result gets the default value as “Hello World “.
Console.WriteLine(result);
Prints result value to console
Course Video
Task:
1. Arithmetic Operators
Write a C# program to perform the following operations and print the results:
Addition: 8 + 4
Subtraction: 15 – 7
Multiplication: 6 * 9
Division: 20 / 4
Modulus: 19 % 4
Create a variable num with a value of 10. Use the increment operator to increase its value by 1, then print the result. Next, use the decrement operator to decrease its value by 1, and print the result.
2. Comparison Operators
Write a C# program to compare two integers, a = 15 and b = 20. Use all the comparison operators (==, !=, >, <, >=, <=) and print the results of each comparison.
3. Logical Operators
Create two boolean variables, isStudent and isMember. Set isStudent to true and isMember to false. Write a C# program to evaluate and print the following logical expressions:
isStudent && isMember
isStudent || isMember
!isStudent
4. Assignment Operators
Start with an integer variable x initialized to 10. Use the assignment operators to perform the following operations and print the value of x after each step:
Add 5 to x
Subtract 3 from x
Multiply x by 2
Divide x by 4
Calculate the modulus of x by 3
5. Conditional Operator
Write a C# program using the conditional operator to:
Compare two integers a = 8 and b = 12, and print which one is greater.
Check if a given number num = 7 is even or odd and print the result.
Determine if a person with age = 16 is an adult or a minor and print the result.
Assign a value to an integer variable value based on a boolean variable isValid
Create a string variable name that is null. Use the null-coalescing operator to assign a default value “Unknown” if name is null, and print the result