C# Final Exp 1 To 8
C# Final Exp 1 To 8
using System;
namespace Lab4
for (int j = 0; j < jag[i].Length; j++) // Loop over columns of current row
for (int j = 0; j < jag[i].Length; j++) // Loop over columns of current row
Console.ReadLine();
using System;
class Program
{
Console.WriteLine("Enter the first number:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
absChild.Add(a,b);
absChild.Sub(a,b);
absChild.Mul(a,b);
absChild.Div(a,b);
{
Console.WriteLine($"Multiplication of {x} and {y} is : {x*y}");
using System;
class Shape
Width = width;
class Program
{
// Circle operations
// Rectangle operations
// Print results
using System;
class Book
class Library
get
else
set
books[index] = value;
else
if (book != null)
class Program
// Create books
Book book2 = new Book { Title = "To Kill a Mockingbird", Author = "Harper Lee" };
Book book3 = new Book { Title = "The Great Gatsby", Author = "F. Scott Fitzgerald" };
library[0] = book1;
library[1] = book2;
library[2] = book3;
library.DisplayBooks();
using System;
n = n - num;
return n;
}
public static int add(int num)
n=n + num;
return n;
n = n * num;
return n;
n = n / num;
return n;
a(10);
c(3);
b(30);
d(5);
return n;
using System;
class MyEvent
if(SomeEvent != null)
SomeEvent();
class EventDemo
Console.WriteLine("Event Occurred");
evt.SomeEvent += Handler;
evt.OnSomeEvent();
}
using System;
private int currentIndex = 0; // Private field for tracking the current index
items[currentIndex++] = item;
Console.WriteLine($"Added: {item}");
else
{
Console.WriteLine("Storage is full!");
return items[index];
else
stringStorage.Add("Apple");
stringStorage.Add("Banana");
intStorage.Add(42);
intStorage.Add(100);
Console.WriteLine($"Item at index 0: {intStorage.Get(0)}");
7) Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
using System;
try
Console.WriteLine($"Result: {result}");
finally
7) Using Try, Catch and Finally blocks write a program in C# to demonstrate errorhandling.
using System;
using System.Linq.Expressions;
using System.IO;
class Program
try
catch (FormatException)
Console.WriteLine("Error:FormatException occurred");
try
int[] array = { 1, 2, 3 };
Console.WriteLine($"Array element at index 5:{array[5]}");
catch (IndexOutOfRangeException)
Console.WriteLine("Error:IndexOUtOFRangeException occurred");
try
File.ReadAllText(FilePath);
catch (FileNotFoundException)
try
Console.WriteLine($"Result = {result}");
catch (DivideByZeroException)
try
{
Console.WriteLine($"An unexpected error occurred:{ex.Message}");
finally
if (divisor == 0)
using System;
using System.Threading;
class Program
producerThread.Start();
consumerThread.Start();
producerThread.Join();
consumerThread.Join();
lock (lockObject)
Console.WriteLine($"Produced:{i}");
sum += i;
dataProduced = true;
Monitor.Pulse(lockObject);
Monitor.Wait(lockObject);
dataProduced = false;
Monitor.Pulse(lockObject);
lock (lockObject)
while (!dataProduced)
Monitor.Wait(lockObject);
while (dataProduced)
Console.WriteLine($"Consumed:{sum}");
Monitor.Pulse(lockObject);
Monitor.Wait(lockObject);