Operators in C#
Learn how operators work in C# with simple explanations, examples, and best practices.
Table of Contents
- What are Operators?
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Unary Operators
- Ternary Operator
- Bitwise Operators
- Operator Precedence
- Summary
What are Operators?
Operators are special symbols that perform operations on variables and values.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | Addition | a+b |
| - | Subtraction | a-b |
| * | Multiplication | a*b |
| / | Division | a/b |
| % | Modulus | a%b |
int a=10,b=3;
Console.WriteLine(a+b);
Console.WriteLine(a-b);
Console.WriteLine(a*b);
Console.WriteLine(a/b);
Console.WriteLine(a%b);
Assignment Operators
| Operator | Meaning |
|---|---|
| = | Assign value |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
Comparison Operators
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Logical Operators
| Operator | Description |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
bool isAdult=true;
bool hasTicket=false;
Console.WriteLine(isAdult && hasTicket);
Console.WriteLine(isAdult || hasTicket);
Console.WriteLine(!hasTicket);
if statements to combine multiple conditions.Unary Operators
| Operator | Description |
|---|---|
| ++ | Increment |
| -- | Decrement |
int count=5;
count++;
count--;
Console.WriteLine(count);
Ternary Operator
int age=20;
string result = age>=18 ? "Adult" : "Minor";
Console.WriteLine(result);
Bitwise Operators
Bitwise operators work on binary values.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
= (assignment) with == (comparison).Operator Precedence
- () Parentheses
- Unary (++ -- !)
- * / %
- + -
- Comparison
- Logical AND
- Logical OR
- Assignment
Summary
- Arithmetic operators perform calculations.
- Comparison operators compare values.
- Logical operators combine conditions.
- Ternary operator is a shorthand for if-else.
Interview Questions
- What is an operator?
- Difference between = and ==?
- What is the ternary operator?
- What are logical operators?
Practice Exercise
- Write a calculator using arithmetic operators.
- Compare two numbers.
- Use && and || in an if statement.
- Create a program using the ternary operator.
User Input in C#
Learn how to accept input from users using Console.ReadLine(), Convert, Parse and TryParse.
Table of Contents
- Introduction
- Console.ReadLine()
- Reading Numbers
- Convert
- Parse
- TryParse
- Best Practices
What is User Input?
User input allows a program to receive data entered from the keyboard during execution.
Console.ReadLine() always returns a string.Reading String Input
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
Reading Integer Input
Console.Write("Enter age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(age);
Reading Double Input
Console.Write("Enter salary: ");
double salary = Convert.ToDouble(Console.ReadLine());
Using Parse()
string value="100";
int number=int.Parse(value);
Using TryParse()
TryParse safely validates input without throwing an exception.
Console.Write("Enter Marks: ");
string input=Console.ReadLine();
if(int.TryParse(input,out int marks))
{
Console.WriteLine("Marks: "+marks);
}
else
{
Console.WriteLine("Invalid number");
}
TryParse() when accepting user input from users.Complete Example
using System;
class Program
{
static void Main()
{
Console.Write("Enter Name: ");
string name=Console.ReadLine();
Console.Write("Enter Age: ");
int age=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Welcome "+name);
Console.WriteLine("Age: "+age);
}
}
Output
Enter Name: John
Enter Age: 25
Welcome John
Age: 25
Convert or Parse can cause runtime exceptions.Comparison
| Method | Use |
|---|---|
| Console.ReadLine() | Read text input |
| Convert | Convert string to another type |
| Parse() | Convert valid strings |
| TryParse() | Safely validate and convert input |
Interview Questions
- What does Console.ReadLine() return?
- Difference between Parse and TryParse?
- Why use Convert?
Practice Exercise
- Read name and city.
- Read age and salary.
- Validate marks using TryParse.
FAQ
Can Console.ReadLine() read numbers?
It reads text. Convert the returned string into a numeric type.
Next Tutorial: if...else Statements in C#
No comments:
Post a Comment