Exercices On C#
Exercices On C#
Exercises on C# Programming
1. Basics of C# Programming
2. Control Flow
Write a program that checks whether a number entered by the user is odd or even
using an if-else statement.
Create a simple program that accepts a grade (A, B, C, D, or F) from the user and
outputs a message based on the grade using a switch statement.
Use a for loop to print the Fibonacci sequence up to a given number.
Write a program using a while loop that calculates the sum of digits of a number
entered by the user.
Create a program to find the largest and smallest numbers in a one-dimensional array.
Write a program to initialize a two-dimensional array and display its elements in a
matrix format.
Use a List<int> to store and display user-entered numbers, and calculate their
average using LINQ.
4. Functions
Write a program to create a method IsPrime() that checks if a given number is prime,
and use this method to print all prime numbers in a range.
Implement a method ReverseString() that takes a string as input and returns the
reversed string.
Create an overloaded version of a method Calculate() that computes the square of
an integer and the cube of a floating-point number.
5. Object-Oriented Programming
Write a C# class called Person with properties Name and Age, and a method
DisplayInfo() that prints the details. Create objects of the class and use them.
Create a program that demonstrates inheritance. Define a base class Vehicle with a
method DisplayDetails(). Derive a Car class and override the method.
Implement a simple banking system using classes with functionalities like deposit,
withdrawal, and balance check.
6. Exception Handling
Write a program to handle DivideByZeroException using try-catch.
Create a program that throws a custom exception when the user inputs an invalid age
(e.g., negative numbers).
Implement a program to demonstrate a finally block, ensuring cleanup of resources
(like file handling).
7. LINQ
Write a program using LINQ to filter and display even numbers from a list of integers.
Create a program to find the maximum, minimum, and average values in an array
using LINQ.
Use LINQ to query a list of strings and display only those that start with a given letter.
8. File Handling
Write a program to create a text file, write a series of numbers into it, and then read
the numbers and display them.
Create a program to count the number of words and lines in a text file.
Write a program to append user-entered text to an existing file.
Write a program to demonstrate a delegate that points to methods for addition and
subtraction.
Create a simple event handling example where an event is raised when a specific
condition is met (e.g., temperature exceeds a threshold).
Implement a program that demonstrates the use of async and await for reading and
writing to files.
Create a program using generics that defines a GenericList<T> class to store and
manage items of any type.
Write a program to demonstrate the use of a Task to execute a piece of code in a
separate thread.
Additional Challenges
1. Write a program to simulate a library management system where books can be added,
borrowed, or returned using classes and collections.
2. Implement a CRUD (Create, Read, Update, Delete) application using a console-based
interface and file handling for storing data persistently.
3. Create a program that uses LINQ to group and sort data from a list of objects (e.g.,
employees grouped by department and sorted by salary).