PROGRAMMING USING C#.NET
Contents
---------------------------------------------------------------------------------------------------------
1. Variables, Constants and Arrays
AIM :-
To write a C# program for Variables, Constants and Arrays using Console Application.
ALGORITHM :-
Step 1 : Start Microsoft Visual Studio.Net.
In the Start menu at All Programs>Microsoft Visual Studio 2022>Microsoft Visual Studio 2022
Step 2: Create a new C# application
In visual studio : Goto the Menu bar, select File>New>Project.
Step 3: In the new popup, Select Visual C# from the left pane and choose Console App.
Step 4: In the Name section give the name ex1 for the project and
·Select an appropriate Location path to save the project files and
·Check the Create directory for solution and
·Click OK.
Step 5 : After clicking the OK button, a new console application will be created.
In case the Program.cs file is not opened in the code editor, Open Solution Explorer menu on the right side
and open your Program.cs file code by double-clicking on it.
Step 6 : Type the program in Program.cs
Step 7 : Compile
Select Build>Build Solution from the VS.Net menu bar. This will create an executable program
Step 8 : Execute
To Run C# Program. Press F5 or Choose Debug->Start Debugging.
Step 9 : View output
To compile and run the application Press either Ctrl + F5 or click on Start option in the menu bar.
PROCEDURE (VARIABLES, CONSTANTS, ARRAYS):-
Step 1: Start the C# Program.
Step 2: Define class.
Step 3: A string message is declared.
Step 4: An integer val is declared and get the value is 30.
Step 5: Use for loop Statement.
Step 6: Write the Value using Console.WriteLine() method.
Step 7: Read the Value using Console.ReadLine() method.
Step 8: Stop the Program
PROGRAM :-
using System;
namespace Program1
{
public class ACV
{
public static void Main(String[] args)
{
int[] arr=new int[5];
arr[0]=100;
arr[2]=200;
arr[4]=300;
String Message="The value is ";
Int32 val=30;
Console.WriteLine("//Arrays in C# Program");
for(int i=0;i<arr.Length;i++)
{
Console.WriteLine(arr[i]);
}
Console.WriteLine("\n//Constants in C# Program\nHello\tKalidhasan\n\n");
Console.WriteLine("//Variables in C# Program");
Console.WriteLine(Message+val);
Console.ReadLine();
}
}
}
CLASS DIAGRAM :-
OUTPUT:-
//Arrays in C# Program
100
0
200
0
300
//Constants in C# Program
Hello Kalidhasan
//Variables in C# Program
The value is 30
---------------------------------------------------------------------------------------------------------
2. Classes and Objects
AIM :-
To write a C# program for Classes and Objects using Console Application.
ALGORITHM :-
Step 1 : Start Microsoft Visual Studio.Net.
In the Start menu at All Programs>Microsoft Visual Studio 2022>Microsoft Visual Studio 2022
Step 2: Create a new C# application
In visual studio : Goto the Menu bar, select File>New>Project.
Step 3: In the new popup, Select Visual C# from the left pane and choose Console App.
Step 4: In the Name section give the name ex1 for the project and
·Select an appropriate Location path to save the project files and
·Check the Create directory for solution and
·Click OK.
Step 5 : After clicking the OK button, a new console application will be created.
In case the Program.cs file is not opened in the code editor, Open Solution Explorer menu on the right
side and open your Program.cs file code by double-clicking on it.
Step 6 : Type the program in Program.cs
Step 7 : Compile
Select Build>Build Solution from the VS.Net menu bar. This will create an executable program
Step 8 : Execute
To Run C# Program. Press F5 or Choose Debug->Start Debugging.
Step 9 : View output
To compile and run the application Press either Ctrl + F5 or click on Start option in the menu bar.
PROCEDURE (Classes and Objects):-
Step 1: Start the C# Program
Step 2: Created a class stud with members name, rollno, mark
Step 3: Create an object Mystud
Step 4: Read the data
Step 5: Print the data and print result by checking if the mark is >=50
Step 6: Write the value using Console.WriteLine() method.
Step 7: Read the value using Console.ReadLine() method.
Step 8: Stop the C# Program
PROGRAM:-
using System;
namespace Program2
{
class stud
{
public string name;
public string rollno;
public int mark;
// Create a class constructor with multiple parameters
public stud(string studname, string studrollno, int studmark)
{
name = studname;
rollno = studrollno;
mark = studmark;
}
// Main Function
static void Main(string[] args)
{
stud Mystud = new stud("Kalidhasan", "8", 91); //Create object
Console.WriteLine("Data updated from the constructor");
Console.WriteLine(Mystud.rollno+" " +Mystud.name + " " +Mystud.mark);
// Reading from the keyboard and update the values
Console.WriteLine("\nEnter Rollno, Name, Mark");
Mystud.rollno = Console.ReadLine();
Mystud.name = Console.ReadLine();
Mystud.mark = Convert.ToInt32(Console.Read());
Console.WriteLine("Data displayed after user entry :");
Console.WriteLine(Mystud.rollno+" " + Mystud.name + " " + Mystud.mark);
if (Mystud.mark>50)
Console.WriteLine("Result : Pass");
else
Console.WriteLine("Result : Fail");
Console.ReadLine();
}
}
}
CLASS DIAGRAM:-
OUTPUT:-
Date updated from the constructor
8 Kalidhasan 91
Enter Rollno, Name, Mark
7
Suresh
60
Data displayed after user entry :
7 Suresh 54
Result : Pass
---------------------------------------------------------------------------------------------------------
3. Inheritance
AIM :-
To write a C# program for Inheritance using Console Application.
ALGORITHM :-
Step 1 : Start Microsoft Visual Studio.Net.
In the Start menu at All Programs>Microsoft Visual Studio 2022>Microsoft Visual Studio 2022
Step 2: Create a new C# application
In visual studio : Goto the Menu bar, select File>New>Project.
Step 3: In the new popup, Select Visual C# from the left pane and choose Console App.
Step 4: In the Name section give the name ex1 for the project and
·Select an appropriate Location path to save the project files and
·Check the Create directory for solution and
·Click OK.
Step 5 : After clicking the OK button, a new console application will be created.
In case the Program.cs file is not opened in the code editor, Open Solution Explorer menu on the right
side and open your Program.cs file code by double-clicking on it.
Step 6 : Type the program in Program.cs
Step 7 : Compile
Select Build>Build Solution from the VS.Net menu bar. This will create an executable program
Step 8 : Execute
To Run C# Program. Press F5 or Choose Debug->Start Debugging.
Step 9 : View output
To compile and run the application Press either Ctrl + F5 or click on Start option in the menu bar.
PROCEDURE (Inheritance):-
Step 1: Start the C# Program
Step 2: Create a base class shape with two methods setLength and setBreadth
Step 3: Create two derived classed rectangle and square
Step 4: Find the area of rectangle as length*breadth
Step 5: Find the area of rectangle as length*length
Step 6: Stop the C# Program
PROGRAM :-
using System;
namespace InheritanceProgram
{
//Base Class
class Shape
{
public void setLength(int w)
{
length = w;
}
public void setBreadth(int h)
{
breadth = h;
}
protected int length;
protected int breadth;
}
// Derived class-1
class Rectangle : Shape
{
public int getArea()
{
return (length * breadth);
}
}
// Derived class-2
class Square : Shape
{
public int getArea()
{
return (length * length);
}
}
class ShapeTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
Rect.setLength(5);
Rect.setBreadth(7);
// Print the area of the Rectangle.
Console.WriteLine("Total area of Rectangle : {0}", Rect.getArea());
Console.ReadKey();
Square sqr = new Square();
sqr.setLength(8);
// Print the area of the Square.
Console.WriteLine("Total area of Square: {0}", sqr.getArea());
Console.ReadKey();
}
}
}
CLASS DIAGRAM:-
OUTPUT:-
Total area of Rectangle : 35
Total area of Square : 64
---------------------------------------------------------------------------------------------------------
4. Polymorphism
AIM :-
To write a C# program for Polymorphism using Console Application.
ALGORITHM :-
Step 1 : Start Microsoft Visual Studio.Net.
In the Start menu at All Programs>Microsoft Visual Studio 2022>Microsoft Visual Studio 2022
Step 2: Create a new C# application
In visual studio : Goto the Menu bar, select File>New>Project.
Step 3: In the new popup, Select Visual C# from the left pane and choose Console App.
Step 4: In the Name section give the name ex1 for the project and
·Select an appropriate Location path to save the project files and
·Check the Create directory for solution and
·Click OK.
Step 5 : After clicking the OK button, a new console application will be created.
In case the Program.cs file is not opened in the code editor, Open Solution Explorer menu on the right
side and open your Program.cs file code by double-clicking on it.
Step 6 : Type the program in Program.cs
Step 7 : Compile
Select Build>Build Solution from the VS.Net menu bar. This will create an executable program
Step 8 : Execute
To Run C# Program. Press F5 or Choose Debug->Start Debugging.
Step 9 : View output
To compile and run the application Press either Ctrl + F5 or click on Start option in the menu bar.
PROCEDURE (Polymorphism):-
Step 1: Start the C# Program
Step 2: Create a base class shape with one method double Area
Step 3: Create Three derived classed Circle, Square and Rectangle
Step 4: Find the area of circle as (22/7) * radius * radius
Step 5: Find the are of square as length * length
Step 6: Find the area of rectangle as height * width
Step 7: Create an object [Circle, Square and Rectangle]
Step 8: Stop the C# Program
PROGRAM :-
using System;
//Base Class
public class shape
{
public virtual double Area()
{
return 0;
}
}
//Derived Class-1
public class Circle : shape
{
public double Radius { get; set; }
public Circle()
{
Radius = 6;
}
public override double Area()
{
return (22.0/7.0) * Radius * Radius ;
}
}
//Derived Class-2
public class Square : shape
{
public double Length { get; set; }
public Square()
{
Length = 6;
}
public override double Area()
{
return Length * Length;
}
}
//Derived Class-3
public class Rectangle : shape
{
public double Height { get; set; }
public double Width { get; set; }
public Rectangle()
{
Height = 5.2;
Width = 2.4;
}
public override double Area()
{
return Height * Width;
}
}
class Program
{
static void Main(string[] args)
{
shape circle = new Circle();
Console.WriteLine("Area of Circle :" + circle.Area());
shape square = new Square();
Console.WriteLine("Area of Square :" + square.Area());
shape rectangle = new Rectangle();
Console.WriteLine("Area of Rectangle :" + rectangle.Area());
Console.ReadLine();
}
}
CLASS DIAGRAM :-
OUTPUT :-
Area of Circle :113.142857142857
Area of Square :36
Area of Rectangle :12.48
0 Comments