KEMBAR78
C # 4 Programs | PDF | C Sharp (Programming Language) | Programming Paradigms
0% found this document useful (0 votes)
16 views4 pages

C # 4 Programs

The document contains C# code examples demonstrating various programming concepts including decision-making statements, threading, subroutines and functions, as well as inheritance and method overriding. Each section provides a brief implementation of the respective concept, showcasing how to use if statements, create and manage threads, define functions and subroutines, and implement inheritance in classes. The code snippets are organized into separate namespaces for clarity and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

C # 4 Programs

The document contains C# code examples demonstrating various programming concepts including decision-making statements, threading, subroutines and functions, as well as inheritance and method overriding. Each section provides a brief implementation of the respective concept, showcasing how to use if statements, create and manage threads, define functions and subroutines, and implement inheritance in classes. The code snippets are organized into separate namespaces for clarity and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11.

25 6:02 AM
C#
1. Demonstrate OOP concept

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

namespace consoleApplication10
{
class program
{
static void Main(String[] args)
{
int a, b, ch, big;
Console.WriteLine("use of dicission making statements ");
Console.WriteLine("1. if statement");
Console.WriteLine("2. if else statement");
Console.WriteLine("3.Exit");

Console.WriteLine("enter your choice");


ch = int.Parse(Console.ReadLine());

switch(ch)
{
case 1: Console.WriteLine("Enter two numbers");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
big = a;
if (b>big)
{
big = b;
}
Console.WriteLine("Biggest of given numbers = "+ big);
break;

case 2: Console.WriteLine("enter two numbers");


a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
if(a>b)
{
Console.WriteLine("biggest of given number is = " + a);
}
else
{
Console.WriteLine("biggets of given number is = "+b);
}
break;
case 3: Environment.Exit(1);
break;

default : Console.WriteLine("enter a value within 1 to 3!");


break;

}
Console.ReadKey();
}
}
}
2. Demonstrate threads

using System;
using System.Threading;

namespace ConsoleApplication11
{
class Program
{
public static void Main(string[] args)
{
Thread ThreadObject1 = new Thread(Example1);
Thread ThreadObject2 = new Thread(Example2);
Thread ThreadObject3 = new Thread(Example3);

ThreadObject1.Start();
ThreadObject2.Start();
ThreadObject3.Start();

Console.ReadKey();
}

static void Example1()


{
Console.WriteLine("Thread1 started");
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("Thread 1 executing");
Thread.Sleep(5000);
}
Console.WriteLine("Thread1 Stopped");
}

static void Example2()


{
Console.WriteLine("Thread2 started");
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("Thread 2 executing");
Thread.Sleep(5000);
}
Console.WriteLine("Thread2 Stopped");
}

static void Example3()


{
Console.WriteLine("Thread3 started");
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("Thread 3 executing");
Thread.Sleep(5000);
}
Console.WriteLine("Thread 3 Stopped");
}
}
}

3. Demonstrate subroutine and function

using System;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int a, b, sum = 0;
Console.WriteLine("Enter two numbers ");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
sum = AddFunction(a, b);
Console.WriteLine("Sum of two numbers (Using functions): " + sum);
AddSubroutine(a, b);
Console.ReadLine();
}

static int AddFunction(int x, int y)


{
return x + y;
}

static void AddSubroutine(int x, int y)


{
Console.Write("Sum of two numbers (Using Subroutines): " + (x + y));
}
}
}

4. Inheritance and over ride

using System;

namespace ConsoleApplication3
{
class Program
{
public static void Main(string[] args)
{
MyBase bobj = new MyBase(10);
MyDerived dobj = new MyDerived(10, 20);

Console.WriteLine("Area of square = " + dobj.Compute(10));


Console.WriteLine("Area of Rectangle = " + dobj.Compute(10, 5));

dobj.VirtualMethod();
dobj.Display();
bobj.Display();

Console.ReadKey();
}
}

public class MyBase


{
public int x;

public MyBase(int x)
{
this.x = x;
}

public virtual void VirtualMethod()


{
Console.WriteLine("Virtual method in base class!");
}

public void Display()


{
Console.WriteLine("Display method of base class!");
}

public int Compute(int x)


{
return (x * x);
}
}

public class MyDerived : MyBase


{
public int y;

public MyDerived(int x, int y) : base(x)


{
this.y = y;
}

public override void VirtualMethod()


{
Console.WriteLine("Virtual method in Derived class!");
}

public new void Display()


{
Console.WriteLine("Display method of Derived class!");
}

public int Compute(int x, int y)


{
return (x * y);
}
}
}

You might also like