0% found this document useful (0 votes)
42 views4 pages

Sodapdf

The document contains 10 code samples demonstrating various C# programming concepts like loops, inheritance, exceptions, interfaces, and more. Each code sample is contained within its own namespace and includes a Main method to run the code.
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)
42 views4 pages

Sodapdf

The document contains 10 code samples demonstrating various C# programming concepts like loops, inheritance, exceptions, interfaces, and more. Each code sample is contained within its own namespace and includes a Main method to run the code.
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

namespace ProgrammingExercise_001

{
class FindOutput
{
static void Main(string[] args)
{
int i = 5;
for (; ; )
{
Console.Write(i + " ");
if (i >= -10)
i -= 5;
else
break;
}
Console.ReadLine();
}
}
}
namespace ProgrammingExercise_002
{
class Program
{
static String location;
static DateTime time;
static void Main(string[] args)
{
Console.WriteLine(location == null ? "location is null" : location);
Console.WriteLine(time == null ? "time is null" : time.ToString());
Console.ReadLine();
}
}
}
namespace ProgrammingExercise_003
{
class Program
{
static void Main(string[] args)
{
Program obj = null;
Console.WriteLine(Program.print());
Console.ReadKey();
}
private static String print()
{
return "Hi, I am a Tech-savvy!!";
}
}
}
namespace ProgrammingExercise_004
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[3];
a[1] = (10 * 2);
Object o = a;
int[] b = (int[])o;
b[1] = 100;
Console.WriteLine(a[1]);
((int[])o)[1] = 1000;
Console.WriteLine(a[1]);
Console.ReadKey();
}
}
}
namespace ProgrammingExercise_005
{
class Program
{
public static void Main(string[] args)
{
Program p = new Program();
p.print(2, 3, 8);
int[] arr = { 2, 11, 15, 20 };
p.print(arr);
Console.ReadLine();
}
public void print(params int[] b)
{
foreach (int i in b)
{
Console.WriteLine(i);
}
}
}
}
namespace ProgrammingExercise_006
{
class Program
{
static void arrayMethod(int[] a)
{
int[] b = new int[5];
a = b;
}
public static void Main(string[] args)
{
int[] arr = new int[2] { 4, 5 };
arrayMethod(arr);
Console.WriteLine(arr.Length);
Console.ReadLine();
}
}
}
namespace ProgrammingExercise_007
{
interface A
{
void func(int i);
}
class B1 : A
{
public int x;
public void func(int i)
{
x = i * i;
}
}
class B2 : A
{
public int x;
public void func(int i)
{
x = i / i;
}
}
class Program
{
public static void Main(string[] args)
{
B1 b1 = new B1();
B2 b2 = new B2();
b1.x = 0;
b2.x = 0;
b1.func(2);
b2.func(2);
Console.WriteLine(b1.x + " " + b2.x);
Console.ReadLine();
}
}
}
namespace ProgrammingExercise_008
{
class Program
{
static void Main(string[] args)
{
int somevalue = 6;
int[] arr = new int[5];
try
{
arr[somevalue] = 100;
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Index out of bounds occurred");
}
Console.WriteLine("Program execution continued after Exception Handling");
}
}
}
namespace ProgrammingExercise_009
{
class Program
{
static void Main(string[] args)
{
int val = 10;
try
{
int i;
for (i = -1; i < 3; ++i)
val = (val / i);
}
catch (ArithmeticException e)
{
Console.WriteLine("0");
}
Console.WriteLine(val);
Console.ReadLine();
}
}
}
namespace ProgrammingExercise_010
{
class BaseSample
{
int i = 10;
int j = 20;
public void print()
{
Console.WriteLine("inside base method ");
}
}
class DerivedSample : BaseSample
{
public int s = 30;
}
class Program
{
static void Main(string[] args)
{
DerivedSample d = new DerivedSample();
Console.WriteLine("{0}, {1}, {2}", d.i, d.j, d.s);
d.print();
Console.ReadLine();
}
}

You might also like