100% found this document useful (1 vote)
390 views30 pages

C#.Net Lab Report

This lab report summarizes programs developed by Shubhra Debnath to fulfill the requirements of the 5th semester MCA course Windows Application Development with C#.NET (18MCA5PCWP) at B.M.S College of Engineering. The programs cover topics like boxing and unboxing, jagged arrays, classes and objects, properties, structs, exception handling, inheritance with sealed classes and methods, abstract classes and interfaces, polymorphism, operator overloading, delegates and events, and LINQ. The lab report was submitted under the guidance of Assistant Professor Dr. Ch Ram Mohan Reddy and certified by the internal guide.

Uploaded by

Shubhra Debnath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (1 vote)
390 views30 pages

C#.Net Lab Report

This lab report summarizes programs developed by Shubhra Debnath to fulfill the requirements of the 5th semester MCA course Windows Application Development with C#.NET (18MCA5PCWP) at B.M.S College of Engineering. The programs cover topics like boxing and unboxing, jagged arrays, classes and objects, properties, structs, exception handling, inheritance with sealed classes and methods, abstract classes and interfaces, polymorphism, operator overloading, delegates and events, and LINQ. The lab report was submitted under the guidance of Assistant Professor Dr. Ch Ram Mohan Reddy and certified by the internal guide.

Uploaded by

Shubhra Debnath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 30

B.M.

S COLLEGE OF ENGINEERING
(Autonomous College under VTU)
Bull Temple Road, Bangalore-560019

DEPARTMENT OF COMPUTER APPLICATIONS

Lab report on
Windows Application Development
with C#.NET (18MCA5PCWP)

Submitted to fulfill the requirements of


5th Semester MCA

Submitted by:
SHUBHRA DEBNATH (1BF19MCA15)

Under the Guidance of


Assistant Professor
Dr Ch Ram Mohan Reddy
1
B.M.S COLLEGE OF ENGINEERING
(Autonomous College under VTU)
Bull Temple Road, Bangalore-560019

DEPARTMENT OF COMPUTER APPLICATIONS

CERTIFICATE
This is to certify that SHUBHRA DEBNATH(1BF19MCA15) has
satisfactorily Windows Application Development with C#.NET
(18MCA5PCWP) completed the requirements Laboratory Report prescribed
by 5th semester MCA course, BMS College of Engineering during the year
2021-2022.

Signature of the Internal Guide


TABLE OF CONTENTS
S.NO PART A

1. Programs to demonstrate Boxing and Unboxing.

2. Program to demonstrate the sum of all the elements present in a jagged array of
3 inner arrays.

3. Programs to demonstrate Classes and Objects

4. Programs to illustrate the use of different properties in C#

5. Programs to demonstrate Exploring Structs.

6. Programs to demonstrate Exception Handling.

7. Program to demonstrate inheritance covering the concepts of Sealed


Classes, Sealed Methods, Extension Methods

8. Programs to demonstrate Abstract Classes and Interfaces.

9. Programs to demonstrate Polymorphism covering the concepts of


Overloading, Overriding, Virtual and Override keywords

10. Programs to demonstrate on Operator Overloading

11. Programs to demonstrate Delegates and Event Handlers.

12. Programs to demonstrate Language Integrated Query (LINQ).


Program1: Program to demonstrate Boxing and Unboxing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P1_Lab1
{
class Program
{
static void Main(string[] args)
{

int num = 23;


object obj = num;
num=100;
Console.WriteLine("Boxing in c# by converting int to object");
Console.WriteLine("Value of num is : " + num);
Console.WriteLine("Value of object is : " + obj);
Console.WriteLine("UnBoxing in c# by converting object to int");
obj=num;
int l=(int) obj;
Console.WriteLine("Value of ob object is : " + obj);
Console.WriteLine("Value of l is : " + l);

}
}

}
OUTPUT:
PROGRAM2: Program to demonstrate the sum of all the elements present in a jagged
array of 3 inner arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Lapprogram2
{
class Program
{
static void Main(string[] args)
{
int r = 3,sum=0;
Console.WriteLine("Sum of all the elements present in a jagged array of 3 inner arrays");
//r=Convert.ToInt32(Console.ReadLine());
int[][] arr = new int[r][];
for (int i = 0; i < r; i++)
{
Console.WriteLine("How many number of elements");
int m = Convert.ToInt32(Console.ReadLine());
arr[i] = new int[m];
Console.WriteLine("Enter" + m + "elements");
for (int j = 0; j < m; j++)
{

arr[i][j] = int.Parse(Console.ReadLine());
sum = sum + arr[i][j];
}
}
Console.WriteLine("The sum of elements in a jagged array of 3 inner arrays are " +
sum);

}
}
}
OUTPUT:
Program 3: Programs to demonstrate Classes and Objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Labprogram3
{
class Program
{
int insuranceId, carnum;
String carname, color;
void getCarDetails()
{
Console.WriteLine("Enter the insuranceId,car number,color,name");
insuranceId = Convert.ToInt32(Console.ReadLine());
carnum = Convert.ToInt32(Console.ReadLine());
color = Console.ReadLine();
carname = Console.ReadLine();
}
void Display()
{
Console.WriteLine("Car Details");
Console.WriteLine(" ");
Console.WriteLine("Car InsuranceID: " + insuranceId);
Console.WriteLine("Car Number: "+carnum);
Console.WriteLine("Car Color: " + color);
Console.WriteLine("Car Name: "+carname);

}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Classes and objects");
p.getCarDetails();
p.Display();
}
}
}
OUTPUT:
PROGRAM4: Program to illustrate the use of different properties in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Labprogram4
{
class Program
{
public String carname;
private int insuranceId=0;
private static int count=0;
public string Car
{
get
{
return carname;
}
set
{
carname = value;

}
}
public override string ToString()
{

return ("Car Name: " + carname);


}
public Program(int PropVal)
{
insuranceId =Convert.ToInt32( PropVal);

}
public int Insurance
{
get
{
return insuranceId;
}

}
public Program()
{
count++;
}
public static int Carnum
{
get
{
return count;
}
set
{
count = value;
}
}
private string[] names = new string[2];
public string this[int i]
{
get
{
return names[i];
}
set
{
names[i] = value;
}
}
static void Main(string[] args)
{
Console.WriteLine("Different properties in C#");
Console.WriteLine(" ");
Program p = new Program();
p.carname = "FORD";
Console.WriteLine("GETTER AND SETTER METHODS");
Console.WriteLine(p);
Console.WriteLine(" ");
Console.WriteLine("ReadOnly Property");
Program p1 = new Program(1234);
Console.WriteLine("Car InsuranceId: " + p1.insuranceId);
Console.WriteLine(" ");
Console.WriteLine("Static Property");
Program p2 = new Program();
Program p3 = new Program();
Console.WriteLine("Number of Objects of Program class using Static property:{0}" +
Program.Carnum);
Console.WriteLine(" ");
Console.WriteLine("Indexer Property");
Program p4 = new Program();
p4[0] = "Rakshitha";
p4[1] = "MCA,BMSCE";
for (int i = 0; i < 2; i++)
{
Console.WriteLine(p4[i]);
}
}
}
}

OUTPUT:
5. Programs to demonstrate Exploring Structs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PROGRAM_LAB5
{
struct area
{
public int l;
public int b;
public area(int l,int b)
{

this.l = l;
this.b = b;

}
public int display()
{
return l * b;
}

};
class Program
{
static void Main(string[] args)
{
area m = new area();
Console.WriteLine("Enter breadth and length of rectangle");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
m.l = a;
m.b = b;
Console.WriteLine("Area of rectangle is {0}", m.display());

}
}
}
OUTPUT:
6. Programs to demonstrate Exception Handling

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationExceptionHandling
{
class DivByZero : Exception
{

// Constructor
public DivByZero()
{
Console.Write("Exception has occurred : ");
}
}
class Program
{
public double DivisionOperation(double numerator,
double denominator)
{
// throw exception when denominator
// value is 0
if (denominator == 0)
throw new DivByZero();

// Otherwise return the result of the division


return numerator / denominator;
}

static void Main(string[] args)


{
Program obj = new Program();
double num, den, quotient;
try
{
// Code block that may cause an exception
Console.Write("Enter Numerartor : ");
num = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Denominator : ");
den = Convert.ToDouble(Console.ReadLine());
quotient = obj.DivisionOperation(num, den);
Console.WriteLine("Quotient = {0}", quotient);
}
// Catch block to catch the generic exception
catch (Exception e)
{
// Message property of exception object e
// will give the specific type of the exception
Console.Write(e.Message);
}
finally
{
Console.ReadLine();
}
}
}
}

OUTPUT:
7.Program to demonstrate inheritance covering the concepts of Sealed Classes, Sealed
Methods, Extension Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationSealed
{
// C# program to
// define Sealed Class
using System;

class Printer
{

// Display Function for


// Dimension printing
public virtual void show()
{
Console.WriteLine("display dimension : 6*6");
}

// Display Function
public virtual void print()
{
Console.WriteLine("printer printing....\n");
}
}

// inheriting class
class LaserJet : Printer
{

// Sealed Display Function


// for Dimension printing
sealed override public void show()
{
Console.WriteLine("display dimension : 12*12");
}

// Function to override
// Print() function
override public void print()
{
Console.WriteLine("Laserjet printer printing....\n");
}
}

// Officejet class cannot override show


// function as it is sealed in LaserJet class.
sealed class Officejet : LaserJet
{

// can not override show function or else


// compiler error : 'Officejet.show()' :
// cannot override inherited member
// 'LaserJet.show()' because it is sealed.
override public void print()
{
Console.WriteLine("Officejet printer printing....");
}
}

// Driver Class
class Program
{

// Driver Code
static void Main(string[] args)
{
Printer p = new Printer();
p.show();
p.print();

Printer ls = new LaserJet();


ls.show();
ls.print();

Printer of = new Officejet();


of.show();
of.print();
Console.ReadLine();
}
}
}

OUTPUT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationExtensionMethod
{
// Here Geek class contains three methods
// Now we want to add two more new methods in it
// Without re-compiling this class
class Geek
{

// Method 1
public void M1()
{
Console.WriteLine("Method Name: M1");
}

// Method 2
public void M2()
{
Console.WriteLine("Method Name: M2");
}

// Method 3
public void M3()
{
Console.WriteLine("Method Name: M3");
}

}
// This class contains M4 and M5 method
// Which we want to add in Geek class.
// NewMethodClass is a static class
static class NewMethodClass
{

// Method 4
public static void M4(this Geek g)
{
Console.WriteLine("Method Name: M4");
}

// Method 5
public static void M5(this Geek g, string str)
{
Console.WriteLine(str);
}
}
// Now we create a new class in which
// Geek class access all the five methods
class Program
{
static void Main(string[] args)
{
Geek g = new Geek();
g.M1();
g.M2();
g.M3();
g.M4();
g.M5("Method Name: M5");
Console.ReadLine();
}
}
}
OUTPUT:
8. Programs to demonstrate Abstract Classes and Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationAbstractClassInterface
{
abstract class Area
{
protected float r, s;
public abstract float area();
}
interface Perimeter
{
float perimeter();
}
class Circle : Area
{
public void input()
{
Console.Write("Enter the value of Radius : ");
r = float.Parse(Console.ReadLine());
}
public override float area()
{
return 3.14F * r * r;
}
}
class Square : Area, Perimeter
{
public void input2()
{
Console.Write("Enter the value of Square Side : ");
s = float.Parse(Console.ReadLine());
}
public override float area()
{
return s * s;
}
public float perimeter()
{
return 4 * s;
}
}
class Program
{
static void Main(string[] args)
{
Circle c = new Circle();
Console.WriteLine("--------Area of Circle---------------");
c.input();
Console.WriteLine("Area : {0}", c.area());
Square s = new Square();
Console.WriteLine("\n-------Area and Perimeter of Square-----------");
s.input2();
Console.WriteLine("Area : {0}", s.area());
Console.WriteLine("Perimeter:{0}", s.perimeter());
Console.Read();
}
}
}

OUTPUT:
9. Programs to demonstrate Polymorphism covering the concepts of Overloading,
Overriding, Virtual and Override keywords

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationOverloadOverride
{
class area
{
public virtual void calcarea(int s) { }
public virtual void calcarea(int l, int b) { }
}
class square : area
{
public override void calcarea(int s)
{
Console.WriteLine("Area of Square : "+(s*s));
}
}
class rectangle : area
{
public override void calcarea(int l, int b)
{
Console.WriteLine("Area of Rectangle : " + (l*b));
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter side of square : ");
int s=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter sides of rectangle : ");
int l = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
area a = new square();
a.calcarea(s);
a = new rectangle();
a.calcarea(l, b);
Console.ReadLine();
}
}
}

OUTPUT:
10.Programs to demonstrate Operator Overloading.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationOperatorOverloading
{
class Cuboid
{
private double length;
private double breadth;
private double height;
public double getVolume()
{
return length * breadth * height;
}
public void setLength(double len)
{
length = len;
}
public void setBreadth(double bre)
{
breadth = bre;
}
public void setHeight(double hei)
{
height = hei;
}
public static Cuboid operator +(Cuboid b, Cuboid c)
{
Cuboid Cuboid = new Cuboid();
Cuboid.length = b.length + c.length;
Cuboid.breadth = b.breadth + c.breadth;
Cuboid.height = b.height + c.height;
return Cuboid;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Volumne of Cuboid Using operator Overloading");
Cuboid Cuboid1 = new Cuboid();
Cuboid Cuboid2 = new Cuboid();
Cuboid Cuboid3 = new Cuboid();
double volume = 0.0;
Console.WriteLine("Enter dimensions of cuboid1 : ");
double a=double.Parse(Console.ReadLine());
double b=double.Parse(Console.ReadLine());
double c = double.Parse(Console.ReadLine());
Console.WriteLine("Enter dimensions of cuboid2 : ");
double x = double.Parse(Console.ReadLine());
double y = double.Parse(Console.ReadLine());
double z = double.Parse(Console.ReadLine());
Cuboid1.setLength(a);
Cuboid1.setBreadth(b);
Cuboid1.setHeight(c);
Cuboid2.setLength(x);
Cuboid2.setBreadth(y);
Cuboid2.setHeight(z);
volume = Cuboid1.getVolume();
Console.WriteLine("Volume of Cuboid1 : {0}", volume);
volume = Cuboid2.getVolume();
Console.WriteLine("Volume of Cuboid2 : {0}", volume);
Cuboid3 = Cuboid1 + Cuboid2;
volume = Cuboid3.getVolume();
Console.WriteLine("Volume of Cuboid3 : {0}", volume);
Console.ReadKey();
}
}
}

OUTPUT:

11.Programs to demonstrate Delegates and Event Handlers.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Lab11
{
public class Program
{
public delegate void DelegateEventHandler();
public static double previous_amount;
public static void Deposit()
{

Console.WriteLine("Enter the amount to grab the bonus");


previous_amount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Current Bank Balance: " + previous_amount);
}
public static void Reward()
{
if (previous_amount >= 5000)
{
double bonus = 500;
Console.WriteLine("Bonus Applied, Bank Balance: " +
(previous_amount + bonus));
}
else
{
Console.WriteLine("No Bonus Applied");
}
}
public static event DelegateEventHandler deh;
public static void Main(string[] args)
{
deh += new DelegateEventHandler(Deposit);
deh += new DelegateEventHandler(Reward);
deh.Invoke();
Console.ReadLine();
}
}
}

OUTPUT:

12. Programs to demonstrate Language Integrated Query (LINQ).


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationLINQ
{
class Program
{
static void Main(string[] args)
{
try {
Console.WriteLine("\n\tUsage of LINQ");
int a;
Console.Write("Enter the size of marks board : ");
a = int.Parse(Console.ReadLine());
int[] marks = new int[a];
Console.WriteLine("Enter the marks : ");
for (int i = 0; i < marks.Length; i++)
{
marks[i] = Convert.ToInt32(Console.ReadLine());
}
IEnumerable<int> marksQuery = from score in marks where
score > 80 select score;
Console.Write("Marks above 80 ");
foreach (int i in marksQuery)
{
Console.Write(i + " ");
}

Console.WriteLine("\n\n***************************\n\n");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}

OUTPUT:

You might also like