Cross_Column

Wednesday, 1 July 2026

Operators and User Input in C#



Operators in C# | Beginner Tutorial

Operators in C#

Learn how operators work in C# with simple explanations, examples, and best practices.

Table of Contents

  1. What are Operators?
  2. Arithmetic Operators
  3. Assignment Operators
  4. Comparison Operators
  5. Logical Operators
  6. Unary Operators
  7. Ternary Operator
  8. Bitwise Operators
  9. Operator Precedence
  10. Summary

What are Operators?

Operators are special symbols that perform operations on variables and values.

Note: Operators are used in calculations, comparisons and decision making.

Arithmetic Operators

OperatorDescriptionExample
+Additiona+b
-Subtractiona-b
*Multiplicationa*b
/Divisiona/b
%Modulusa%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

OperatorMeaning
=Assign value
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign

Comparison Operators

OperatorDescription
==Equal to
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Logical Operators

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT
bool isAdult=true;
bool hasTicket=false;

Console.WriteLine(isAdult && hasTicket);
Console.WriteLine(isAdult || hasTicket);
Console.WriteLine(!hasTicket);
Tip: Use logical operators inside if statements to combine multiple conditions.

Unary Operators

OperatorDescription
++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.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift
Common Mistake: Don't confuse = (assignment) with == (comparison).

Operator Precedence

  1. () Parentheses
  2. Unary (++ -- !)
  3. * / %
  4. + -
  5. Comparison
  6. Logical AND
  7. Logical OR
  8. Assignment

Summary

  • Arithmetic operators perform calculations.
  • Comparison operators compare values.
  • Logical operators combine conditions.
  • Ternary operator is a shorthand for if-else.

Interview Questions

  1. What is an operator?
  2. Difference between = and ==?
  3. What is the ternary operator?
  4. 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# | Beginner Tutorial

User Input in C#

Learn how to accept input from users using Console.ReadLine(), Convert, Parse and TryParse.

Table of Contents

  1. Introduction
  2. Console.ReadLine()
  3. Reading Numbers
  4. Convert
  5. Parse
  6. TryParse
  7. Best Practices

What is User Input?

User input allows a program to receive data entered from the keyboard during execution.

Note: 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");
}
Tip: Prefer 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
Common Mistake: Directly converting invalid input using Convert or Parse can cause runtime exceptions.

Comparison

MethodUse
Console.ReadLine()Read text input
ConvertConvert string to another type
Parse()Convert valid strings
TryParse()Safely validate and convert input

Interview Questions

  1. What does Console.ReadLine() return?
  2. Difference between Parse and TryParse?
  3. 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

Few More

Recursion in C#

Recursion in C# | Beginner Tutorial Recursion in C# Learn how recursion works in C#, understand the base case and re...