OOPS BASIC PROGRAM IN C#
CLASS PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("this program is class example");
Console.ReadLine();
}
}
}
OUTPUT:
this program is class example
OBJECT PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
public void show()
{
Console.WriteLine("this program is class example");
}
static void Main(string[] args)
{
Program prg = new Program();
prg.show();
Console.ReadLine();
}
}
}
OUTPUT:
this program is class example
IF STATEMENT:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int number;
Console.WriteLine("please enter ur number:");
number = Int32.Parse(Console.ReadLine());
if (number < 60)
{
Console.WriteLine("your number {0} is lessthan 60",number );
Console.ReadLine();
}
if (number > 60)
{
Console.WriteLine("your number {0} is morethan 60",number);
Console.ReadLine();
}
}
}
}
OUTPUT:
please enter ur number:34
your number 34 is lessthan 60
IF..ELSE STATEMENT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int number;
Console.WriteLine("please enter ur number:");
number = Int32.Parse(Console.ReadLine());
if (number < 60)
{
Console.WriteLine("your number {0} is lessthan 60",number );
Console.ReadLine();
}
else
{
Console.WriteLine("your number {0} is morethan 60",number);
Console.ReadLine();
}
}
}
}
OUTPUT:
please enter ur number:34
your number 34 is lessthan 60
FIND THE LARGEST NUMBER:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int number1;
int number2;
int number3;
Console.WriteLine("finding the largest number");
Console.WriteLine("please enter first number:");
number1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("please enter second number:");
number2 = Int32.Parse(Console.ReadLine());
Console.WriteLine("please enter third number:");
number3 = Int32.Parse(Console.ReadLine());
if (number1 > number2 && number1 > number3)
{
Console.WriteLine("first nuber is largest number");
Console.ReadLine();
}
if (number2 > number3)
{
Console.WriteLine("second nuber is largest number");
Console.ReadLine();
}
else
{
Console.WriteLine("third nuber is largest number");
Console.ReadLine();
}
}
}
}
SWITCH CASE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string name;
int marks;
Console.WriteLine("please enter ur name:");
name = Console.ReadLine();
Console.WriteLine("select ur range marks:");
Console.WriteLine("1. 0-50");
Console.WriteLine("2. 50-100");
Console.WriteLine("select ur choice(1,2):");
marks = Int32.Parse(Console.ReadLine());
switch (marks)
{
case 1:
{
Console.WriteLine(name + "ur mark is very low.\n");
Console.ReadLine();
} break;
case 2:
{
Console.WriteLine(name + "ur mark is very high.\n");
Console.ReadLine();
}
break;
default:
break;
}
Console.WriteLine("\n press enter the quit...");
Console.ReadLine();
}
}
}
WHILE STATEMENT:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("THIS IS FIRST LOOP");
int i=0;
while(i<5)
{
Console.WriteLine(" this is c#",i);
i++;
}
Console.ReadLine();
}
}
}
DO..WHILE LOOP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("THIS IS FIRST LOOP");
int i=0;
do
{
Console.WriteLine(" this is c#", i);
i++;
}
while (i < 5);
{
}
Console.ReadLine();
}
}
}
FOR STATEMENT:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int i;
for(i=0;i<5;i++)
{
Console.WriteLine(" this is c# for statement", i);
}
Console.ReadLine();
}
}
}
JUMP STATE MENT:BREAK STATEMENT:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(" this number {0}", i);
if (i == 7)
{
break;
}
}
Console.ReadLine();
}
}
}
CONTINUE STATEMENT:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int i = 1;
while(true)
{
Console.WriteLine(" this number {0}", i);
i++;
if (i <= 7)
continue;
else
break;
}
Console.WriteLine("series is breaked at numbe:" + i);
Console.ReadLine();
}
}
}
TRY…CATCH
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string numbers;
int x;
int y;
int z;
Console.WriteLine("enter value x:");
numbers = (Console.ReadLine());
x = Int32.Parse(numbers);
Console.WriteLine("enter value y:");
numbers = (Console.ReadLine());
y = Int32.Parse(numbers);
try
{
z = x / y;
Console.WriteLine("this z value is:{0}", z);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("exception occured" + ex);
}
Console.ReadLine();
}
}
}
ENCAPSULATION:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class myclass
{
private void add()
{
Console.WriteLine("this is private method");
}
public void sub()
{
Console.WriteLine("this is public method");
}
protected void mul()
{
Console.WriteLine("this is protected method");
}
}
class Program:myclass
{
static void Main(string[] args)
{
Program pr = new Program();
pr.mul();
pr.sub();
Console.ReadLine();
}
}
}
OUTPUT
this is protected method
this is public method
OVER RIDING:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Class1
{
public virtual void Hello()
{
Console.WriteLine ("Hello from Class1");
}
}
class Class2 : Class1
{
public override void Hello()
{
base.Hello();
Console.WriteLine (" and hello from Class2 too");
}
public static void Main(string[] args)
{
Class2 c2 = new Class2();
c2.Hello();
Console.ReadLine();
}
}
}
METHOD OVERLODING:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class shape
{
public void area(int side)
{
int squrearea = side * side;
Console.WriteLine("this squre area is:" + squrearea);
}
public void area(int length,int breath)
{
int rectanglearea = length * breath;
Console.WriteLine("this rectangle area is:" + rectanglearea);
}
}
class Program
{
static void Main(string[] args)
{
shape sh = new shape();
Console.WriteLine("this squre area is:" );
int num = Int32.Parse(Console.ReadLine());
sh.area(num );
Console.WriteLine("this length area is:");
int num1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("this brath area is:");
int num2 = Int32.Parse(Console.ReadLine());
sh.area(num1,num2 );
Console.ReadLine();
}
}
}
OUTPUT:
this squre area is:5
this squre area is:25
this length area is:3
this brath area is:4
this rectangle area is:12