OOPS Programming in C#
OOPS Programming in C#
I have created a sample console application which lists all oops related examples like types,
inheritance, interface, sealed, delegates, enum, threading, linq, regular expressions, extension
methods, generics, arrays, list...so on.
Just copy and run your application in console and see the output..
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace CSharpAndMe
{
class Program
{
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
//TODO : Do while
//Defination: It is similar to a while statement, except that it tests the
condition at the end of the loop body
Console.WriteLine("----------------Do While----------------");
int x = 1;
do
{
Console.Write(x + " ");
x++;
} while (x <= 10);
Console.WriteLine();
//TODO : Foreach
//loops through the collection of items or objects
Console.WriteLine("--------------Foreach----------------");
string[] arr = { "sd", "md", "kd" };
foreach (var item in arr)
{
Console.Write(item + " ");
}
Console.WriteLine();
//TODO : switch
//Defination: A switch statement allows a variable to be tested for equality
against a list of values.
//Each value is called a case, and the variable being switched on is checked
for each switch case.
var color = "Red";
Console.WriteLine("----------------Swith Case----------------");
switch (color)
{
case "Red":
Console.WriteLine("Color in Color is " + color);
break;
case "Pink":
Console.WriteLine("Color in Color is " + color);
break;
case "Yellow":
Console.WriteLine("Color in Color is " + color);
break;
default:
break;
}
int n = int.Parse("855");
int d = 0;
string res = "";
string[] ones = new string[] { "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string[] tens = new string[] { "Ten", "Twenty", "Thirty", "Fourty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninty" };
Console.WriteLine("------------Singe Dimensional Array-----------------");
Console.WriteLine();
Console.Write("Sorted Content: ");
al.Sort();
foreach (int i in al)
{
Console.Write(i + " ");
}
Console.WriteLine();
//TODO: Generics
//Defincation:Generics allow you to delay the specification of the data type
of programming elements in a class or a method,
//until it is actually used in the program. In other words, generics allow you
to write a class or method that can work with any data type.
//declaring an int array
MyGenericArray<int> intArray = new MyGenericArray<int>(5);
//setting values
for (int c = 0; c < 5; c++)
{
intArray.setItem(c, c * 5);
}
//retrieving the values
for (int c = 0; c < 5; c++)
{
Console.Write(intArray.getItem(c) + " ");
}
Console.WriteLine();
//declaring a character array
MyGenericArray<char> charArray = new MyGenericArray<char>(5);
//setting values
for (int c = 0; c < 5; c++)
{
charArray.setItem(c, (char)(c + 97));
}
//retrieving the values
SYED KHALEEL
for (int c = 0; c < 5; c++)
{
Console.Write(charArray.getItem(c) + " ");
}
Console.WriteLine();
//TODO: Constructor
//Defination: A constructor is a method of the class,
//i.e. meant for initializing the data members. The constructor gets called
automatically, whenever the object is declared.
Sample obj1 = new Sample(new Sample()
{
EmpId = 51581,
ENmae = "Syed Khaleel",
EAdress = "Hyd",
EAge = 29
});
obj1.displayEmpData();
//TODO:calculate salary
//TODO: Properties
//Defination: Properties are named members of classes, structures, and
interfaces. Member variables or methods in a class or structures are called Fields.
//Properties are an extension of fields and are accessed using the same
syntax. They use accessors through which the values of the private fields can be read,
written or manipulated
Console.WriteLine("------------------------------- Properties ---------------
-------------------");
Property_Employee Property_Employee = new Property_Employee();
Console.Write("Enter Employee Details Id,Name,Address,Age");
Property_Employee.PEmpId = Convert.ToInt32(Console.ReadLine());
Property_Employee.PEName = Console.ReadLine();
Property_Employee.PEAddress = Console.ReadLine();
Property_Employee.PEAge = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Employee Id is " + Property_Employee.PEmpId);
Console.WriteLine(" Employee Name is " + Property_Employee.PEName);
Console.WriteLine(" Employee Address is " +
Property_Employee.PEAddress);
Console.WriteLine(" Employee Age is " + Property_Employee.PEAge);
//TODO: Delegate
Console.WriteLine("--------------------Delegate-----------------------------
");
DelegateDemo delDemo = new DelegateDemo();
CSharpAndMe.Program.DelegateDemo.sayDel sd = new
CSharpAndMe.Program.DelegateDemo.sayDel(delDemo.sayHello);
Console.WriteLine(sd("xxx"));
CSharpAndMe.Program.DelegateDemo.addDel ad = new
CSharpAndMe.Program.DelegateDemo.addDel(delDemo.add);
ad(10, 20);
//mathematical programs
//TODO: Generating Febonaci Series
Console.WriteLine("Febonaci Series");
int fi, _count, f1 = 0, f2 = 1, f3 = 0;
Console.Write("Enter the Limit : ");
_count = int.Parse(Console.ReadLine());
Console.WriteLine(f1);
Console.WriteLine(f2);
for (fi = 0; fi <= _count; fi++)
{
f3 = f1 + f2;
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
//Armstrong Number
//TODO: Finding Armstrong Number
Console.WriteLine("Armstrong Number");
int numberr, remainder, sum = 0;
Console.Write("enter the Number");
numberr = int.Parse(Console.ReadLine());
for (int i = numberr; i > 0; i = i / 10)
{
remainder = i % 10;
sum = sum + remainder * remainder * remainder;
}
if (sum == numberr)
{
Console.Write("Entered Number is an Armstrong Number");
}
else
Console.Write("Entered Number is not an Armstrong Number");
//Perfect Number
//TODO: Perfect Number Example
Console.WriteLine("Perfect Number");
//Palindrom
//TODO: to find the palindrome of a number
Console.WriteLine("Palindrome of a given number");
int _num, temp, remainderr, reverse = 0;
Console.WriteLine("Enter an integer \n");
_num = int.Parse(Console.ReadLine());
temp = _num;
while (_num > 0)
{
remainderr = _num % 10;
reverse = reverse * 10 + remainderr;
_num /= 10;
SYED KHALEEL
}
Console.WriteLine("Given number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
if (temp == reverse)
Console.WriteLine("Number is a palindrome \n");
else
Console.WriteLine("Number is not a palindrome \n");
Console.WriteLine("Prime Number");
//Reverse of a string
Console.ReadKey();
}
}
public ClsConstrutorOverloading(int Id, string S1)
SYED KHALEEL
{
EmpId = Id;
EName = S1;
}
public ClsConstrutorOverloading(int Id, string S1, string S2, int Ag)
{
EmpId = Id;
EName = S1;
EAddress = S2;
EAge = Ag;
}
public void DisplayEmpData()
{
Console.WriteLine("---------------------------------- Constructor
Overloading --------------------------------------------------");
Console.WriteLine("Employee id is " + EmpId);
Console.WriteLine("Employee Name is " + EName);
Console.WriteLine("Employee Adress is " + EAddress);
Console.WriteLine("Employee Age is " + EAge);
}
}
class ClsSalary
{
int EmpId;
string Ename;
double basic, Da, Hra, Gross;
public void GetSalData()
{
Console.WriteLine("------------------ Salary --------------------------
");
Console.Write("Enter Employee details Employee Id,Name,Basic");
this.EmpId = Convert.ToInt32(Console.ReadLine());
this.Ename = Console.ReadLine();
this.basic = Convert.ToDouble(Console.ReadLine());
}
public void Calculate()
{
this.Da = 0.4 * this.basic;
this.Hra = 0.3 * this.basic;
this.Gross = this.basic + this.Da + this.Hra;
}
public void DisplaySqldata()
{
Console.WriteLine("Employee Id is " + this.EmpId);
Console.WriteLine("Employee Nameis " + this.Ename);
Console.WriteLine("Employee basic is " + this.basic);
Console.WriteLine("Employee da is " + this.Da);
Console.WriteLine("Employee hra is " + this.Hra);
Console.WriteLine("Employee Gross is " + this.Gross);
}
}
class Branch
{
int Bcode;
string BName, BAddress;
public void GetBData()
{
Console.WriteLine("Enter Branch Details Code,Name,Address");
Bcode = Convert.ToInt32(Console.ReadLine());
BName = Console.ReadLine();
BAddress = Console.ReadLine();
}
public void DisplayBData()
{
Console.WriteLine(" Branch Code is " + Bcode);
Console.WriteLine(" Branch BName is " + BName);
Console.WriteLine(" Branch BAddress is " + BAddress);
}
SYED KHALEEL
}
interface Employee
{
void GetEmpData();
void DisplayEmpData();
}
class Manager : Branch, Employee
{
int EmpId;
string EName;
double Bonus, CA;
public void GetEmpData()
{
Console.WriteLine("Enter Manager Details Id,Name,Bonus,CA");
EmpId = Convert.ToInt32(Console.ReadLine());
EName = Console.ReadLine();
Bonus = Convert.ToDouble(Console.ReadLine());
CA = Convert.ToDouble(Console.ReadLine());
}
public void DisplayEmpData()
{
Console.WriteLine("Manager id is " + EmpId);
Console.WriteLine("Manager Name is " + EName);
Console.WriteLine("Manager Bonus is " + Bonus);
Console.WriteLine("Manager CA is " + CA);
}
}
interface I1
{
void F1();
}
interface I2
{
void F1();
}
class C3 : I1, I2
{
void I1.F1()
{
Console.WriteLine("Method f1 from I1");
}
void I2.F1()
{
Console.WriteLine("Method F2 from I2");
}
class Property_Employee
{
int EmpId, EAge;
string EName, EAddress;
public int PEmpId
{
set { EmpId = value; }
get { return EmpId; }
}
public int PEAge
{
set { EAge = value; }
get { return EAge; }
}
public string PEName
{
set { EName = value; }
get { return EName; }
}
public string PEAddress
{
set { EAddress = value; }
get { return EAddress; }
}
}
class ClsEmployee
{
protected int Empid; int Eage;
protected string Ename; string Eaddress;
public virtual void GetEmpdata()
{
Console.Write("Enter Empliyee Details Id,Name,Address,Age:-");
this.Empid = Convert.ToInt32(Console.ReadLine());
this.Ename = Console.ReadLine();
this.Eaddress = Console.ReadLine();
this.Eage = Convert.ToInt32(Console.ReadLine());
}
public virtual void DisplayEmpData()
{
Console.WriteLine("Employee id is:-" + this.Empid);
Console.WriteLine("Employee id is:-" + this.Ename);
Console.WriteLine("Employee id is:-" + this.Eaddress);
Console.WriteLine("Employee id is:-" + this.Eage);
}
}
sealed class clsmanager : ClsEmployee
{
double bonus, ca;
SYED KHALEEL
public override void GetEmpdata()
{
Console.Write("Enter Empliyee Details Id,Name,Bonus,CA:-");
Empid = Convert.ToInt32(Console.ReadLine());
Ename = Console.ReadLine();
bonus = Convert.ToDouble(Console.ReadLine());
ca = Convert.ToDouble(Console.ReadLine());
}
public override void DisplayEmpData()
{
Console.WriteLine("Manager id is:-" + Empid);
Console.WriteLine("Manager name is:-" + Ename);
Console.WriteLine("Manager bonus is:-" + bonus);
Console.WriteLine("Manager ca is:-" + ca);
}
}
//TODO:Delegates
//Defination: C# delegates are similar to pointers to functions, in C or C++.
//A delegate is a reference type variable that holds the reference to a method.
The reference can be changed at runtime.
//Delegates are especially used for implementing events and the call-back
methods.
//All delegates are implicitly derived from the System.Delegate class.
class DelegateDemo
{
public delegate string sayDel(string name);
public delegate void addDel(int x, int y);
//TODO: Structures
//Defination: In C#, a structure is a value type data type. It helps you to make
a single variable hold related data of various data types.
//The struct keyword is used for creating a structure.
struct MyStruct
{
public int x;
public MyStruct(int x)
{
this.x = x;
}
public void show()
{
SYED KHALEEL
Console.WriteLine("Method in structure : " + x);
}
}
return allinfo;
}
}
}
public void Stop()
{
flag = true;
}
}
public class abbrevation
{
SYED KHALEEL
string str;
public void readdata()
{
Console.WriteLine("Enter a String :");
str = Console.In.ReadLine();
}
public void abbre()
{
char[] c, result;
int j = 0;
c = new char[str.Length];
result = new char[str.Length];
c = str.ToCharArray();
result[j++] = (char)((int)c[0] ^ 32);
result[j++] = '.';
for (int i = 0; i < str.Length - 1; i++)
{
if (c[i] == ' ' || c[i] == '\t' || c[i] == '\n')
{
int k = (int)c[i + 1] ^ 32;
result[j++] = (char)k;
result[j++] = '.';
}
}
Console.Write("The Abbreviation for {0} is ", str);
Console.WriteLine(result);
Console.ReadLine();
}
}
class FileRead
{