💻 C# Concepts & Examples (Interview Ready)
This section covers important C# coding examples including LINQ, Recursion, Sorting, OOP, Delegates, and Algorithms — all commonly asked in interviews.
🔢 Filter & Sort Numbers Using LINQ
📌 Problem
Filter numbers greater than 10 and sort them in descending order.
✅ Solution
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] arr = {1,2,4,56,8,11,23,54,28};
var orderedRecords = arr.Where(x => x > 10).OrderByDescending(num => num);
Console.WriteLine(String.Join(",",orderedRecords));
}
}
🔁 Factorial Calculation
📌 Problem
Calculate factorial of a number using iteration.
✅ Solution
using System;
public class Program
{
public static int factorialCalculation(int input)
{
if(input <= 1)
{
return 1;
}
else
{
int result = 1;
for(int i = 2; i <= input; i++)
{
result = result * i;
}
return result;
}
}
public static void Main()
{
Console.WriteLine("Enter the number:");
int val = int.Parse(Console.ReadLine());
int factVal = factorialCalculation(val);
Console.WriteLine("The Fact is: " + factVal);
}
}
🔄 Bubble Sort Algorithm
📌 Problem
Sort an array using Bubble Sort.
✅ Solution
using System;
public class Program
{ // Bubble Sort
public static int[] sortAnArray(int[] inputArr)
{
for(int i = 0; i < inputArr.Length; i++)
{
for(int j = 0; j < inputArr.Length - 1; j++)
{
if(inputArr[j] > inputArr[j+1])
{
int temp = inputArr[j+1];
inputArr[j+1] = inputArr[j];
inputArr[j] = temp;
}
}
}
return inputArr;
}
public static void Main()
{
int[] arr = {3,45,76,1,24,90,44};
int[] sortedArray = sortAnArray(arr);
Console.WriteLine("The Sorted Array is: " + String.Join(",",sortedArray));
}
}
🚗 Interface Implementation
📌 Concept
Demonstrates abstraction using interfaces.
✅ Solution
using System;
public class Program
{ // Interface Implementation
public static void Main()
{
IVehicle vehicle = new Car();
vehicle.StartEngine();
vehicle.StopEngine();
}
}
public class Car : IVehicle
{
public void StartEngine()
{
Console.WriteLine("Started Engine");
}
public void StopEngine()
{
Console.WriteLine("Stop Engine");
}
}
public interface IVehicle
{
void StartEngine();
void StopEngine();
}
🔢 Prime Numbers
📌 Problem
Print all prime numbers up to a given number.
✅ Solution
using System;
public class Program
{
public static bool IsPrime(int input)
{
if(input <= 1)
return false;
for(int i= 2; i < input; i++)
{
if(input % i == 0)
{
return false;
}
}
return true;
}
// Prime Numbers
public static void Main()
{
int input = int.Parse(Console.ReadLine());
for(int i = 2; i <= input; i++)
{
if(IsPrime(i))
{
Console.WriteLine(i + " ");
}
}
}
}
🔁 Delegates (Custom)
📌 Concept
Demonstrates multicast delegates.
✅ Solution
using System;
public class Program
{
public delegate void MultiOperations(int x, int y);
public static void Add(int a, int b)
{
Console.WriteLine("Addition:" + (a + b));
}
public static void Subtract(int a, int b)
{
Console.WriteLine("Subtraction:" + (a - b));
}
public static void Multiply(int a, int b)
{
Console.WriteLine("Multiplication:" + (a * b));
}
public static void Divide(int a, int b)
{
if (b != 0)
{
Console.WriteLine("Division:" + (a / b));
}
else
{
Console.WriteLine("Division by zero is not allowed.");
}
}
// Delegates
public static void Main()
{
MultiOperations multiOperations = Add;
multiOperations += Subtract;
multiOperations += Multiply;
multiOperations += Divide;
Console.WriteLine("Performing operations on 20 and 5:");
multiOperations(20, 5);
}
}
⚡ Built-in Delegates (Func, Action, Predicate)
📌 Concept
Modern way of using delegates in C#.
✅ Solution
using System;
public class Program
{
// Built-in Delegates - Func, Action, Predicate
public static void Main()
{
Func<int, int, int> Add = (a, b) => a + b;
Action<string> greet = (a) => Console.WriteLine("Good Morning " + a);
Predicate<int> isEven = x => x % 2 == 0;
Console.WriteLine(Add(10,23));
greet("Chitransh");
Console.WriteLine(isEven(16));
}
}
🔢 Fibonacci Series
📌 Problem
Generate Fibonacci sequence up to n terms.
✅ Solution
using System;
public class Program
{
// Fibonacci
public static void Main()
{
int n = 10;
int[] fibbo = new int[n];
fibbo[0] = 0;
fibbo[1] = 1;
for(int i= 0; i < n - 2; i++)
{
fibbo[i+2] = fibbo[i] + fibbo[i+1];
}
Console.WriteLine(String.Join(",",fibbo));
}
}
🧠 Key Takeaways
LINQ simplifies filtering and sorting operations.
Iterative logic is important for problems like factorial and Fibonacci.
Sorting algorithms like Bubble Sort help understand fundamentals.
Interfaces enable abstraction and loose coupling.
Delegates (custom & built-in) are powerful for method referencing.
Prime number logic is a common interview problem.
These examples cover core C# fundamentals + problem-solving skills, making them highly valuable for interviews and real-world development.
Comments
Post a Comment