CODE
using System;
class Break_Continue
{
static void Main()
{
for(int i = 1 ; i <= 10 ; i++)
{
if(i == 3)
{
continue;
}
else if(i == 6)
{
break;
}
Console.WriteLine(i);
}
}
}
OUTPUT
1
2
4
5
CODE
using System;
class Student
{
public string Name { get; set; }
public int RollNo { get; set; }
public void Display()
{
Console.WriteLine("Name: " + Name + " , Roll No: " + RollNo);
}
}
class StudentDetails
{
static void Main()
{
Student s1 = new Student();
s1.Name = "Raj";
s1.RollNo = 101;
Student s2 = new Student();
s2.Name = "Amit";
s2.RollNo = 102;
Student s3 = new Student();
s3.Name = "Priya";
s3.RollNo = 103;
Console.WriteLine("Student Details:");
s1.Display();
s2.Display();
s3.Display();
}
}
OUTPUT
Student Details: Name: Raj , Roll No: 101 Name: Amit , Roll No: 102 Name: Priya , Roll No: 103
CODE
using System;
class Employee
{
private string name;
private double salary;
public string Name
{
get { return name; }
set { name = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
}
class EncapsulationDemo
{
static void Main()
{
Employee emp = new Employee();
emp.Name = "John";
emp.Salary = 50000;
Console.WriteLine("Name: " + emp.Name);
Console.WriteLine("Salary: " + emp.Salary);
}
}
OUTPUT
Name: John
Salary: 50000
CODE
using System;
class Box
{
public int Length;
public int Width;
public Box()
{
Length = 1;
Width = 1;
}
public Box(int l, int w)
{
Length = l;
Width = w;
}
}
class ConstructorDemo
{
static void Main()
{
Box b1 = new Box();
Box b2 = new Box(10, 20);
Console.WriteLine("Box 1: " + b1.Length + "x" + b1.Width);
Console.WriteLine("Box 2: " + b2.Length + "x" + b2.Width);
}
}
OUTPUT
Box 1: 1x1
Box 2: 10x20
CODE
using System;
class Rectangle
{
public int Area;
public Rectangle()
{
Area = 0;
}
public Rectangle(int side)
{
Area = side * side;
}
public Rectangle(int l, int w)
{
Area = l * w;
}
}
class ConstructorOverloading
{
static void Main()
{
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(5);
Rectangle r3 = new Rectangle(4, 6);
Console.WriteLine("Area 1: " + r1.Area);
Console.WriteLine("Area 2: " + r2.Area);
Console.WriteLine("Area 3: " + r3.Area);
}
}
OUTPUT
Area 1: 0
Area 2: 25
Area 3: 24
CODE
using System;
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
class MethodOverloading
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(10, 20));
Console.WriteLine(calc.Add(10.5, 20.5));
Console.WriteLine(calc.Add(10, 20, 30));
}
}
OUTPUT
30
31
60
CODE
using System;
class Complex
{
public int Real;
public int Imag;
public Complex(int r, int i)
{
Real = r;
Imag = i;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.Real + c2.Real, c1.Imag + c2.Imag);
}
public void Display()
{
Console.WriteLine(Real + " + " + Imag + "i");
}
}
class OperatorOverloading
{
static void Main()
{
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, 2);
Complex c3 = c1 + c2;
c3.Display();
}
}
OUTPUT
4 + 6i
CODE
using System;
class Person
{
public string Name;
}
class Student : Person
{
public int Marks;
}
class Teacher : Person
{
public string Subject;
}
class InheritanceDemo
{
static void Main()
{
Student s = new Student();
s.Name = "Alice";
s.Marks = 90;
Teacher t = new Teacher();
t.Name = "Mr. Bob";
t.Subject = "Math";
Console.WriteLine("Student: " + s.Name + ", Marks: " + s.Marks);
Console.WriteLine("Teacher: " + t.Name + ", Subject: " + t.Subject);
}
}
OUTPUT
Student: Alice, Marks: 90
Teacher: Mr. Bob, Subject: Math
CODE
using System;
class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog Barks");
}
}
class OverridingDemo
{
static void Main()
{
Animal myDog = new Dog();
myDog.Sound();
}
}
OUTPUT
Dog Barks
CODE
using System;
abstract class Shape
{
public abstract void Area();
}
class Circle : Shape
{
public override void Area()
{
Console.WriteLine("Circle Area Calculation");
}
}
class Rectangle : Shape
{
public override void Area()
{
Console.WriteLine("Rectangle Area Calculation");
}
}
class AbstractClassDemo
{
static void Main()
{
Shape s1 = new Circle();
Shape s2 = new Rectangle();
s1.Area();
s2.Area();
}
}
OUTPUT
Circle Area Calculation
Rectangle Area Calculation
No comments:
Post a Comment