0% found this document useful (0 votes)
45 views

Programs

The document contains code examples demonstrating various C# programming concepts including enums, ternary operators, regions, checked and unchecked arithmetic, out parameters, reference parameters, the sealed keyword, casting with 'as', constants with boolean datatypes, properties, indexers, and the protected keyword.

Uploaded by

Zainab Muneer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Programs

The document contains code examples demonstrating various C# programming concepts including enums, ternary operators, regions, checked and unchecked arithmetic, out parameters, reference parameters, the sealed keyword, casting with 'as', constants with boolean datatypes, properties, indexers, and the protected keyword.

Uploaded by

Zainab Muneer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Enum:

CODING

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

namespace ConsoleApplication1
{
class Program
{
enum Temperature
{
Wickedcold=0,
FreezingPoint=32,
LightJacketWeather=60,
SwimmingWeather=72,
BoilingPoint=212,
}
static void Main(string[] args)
{
if ((int)Temperature.FreezingPoint==0)
{
Console.WriteLine("FreezingPoint=0");
}
else if((int)Temperature.BoilingPoint == 212)
{
Console.WriteLine("BoilingPoint = 212");
}
Console.ReadLine();
}
}
}

OUTPUT

BoilingPoint = 212

Ternary Operator:

CODING

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int age1 = 40;
int age2 = 50;
int age = age1 > age2 ? age1 : age2;
Console.WriteLine(age);
Console.ReadLine();
}
}
}

OUTPUT

50

Create table with region:

CODING

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

namespace ConsoleApplication1
{
class Program
{
#region "Table"
/// <summary>
/// this is our table pgm
/// </summary>
/// <param name="args"></param>
#endregion
static void Main(string[] args)
{
int a=5;
for (int b = 1; b <= 10; b++)
{
int c = a * b;
Console.WriteLine("5 * "+b+"="+c);

}
Console.ReadLine();
}
}
}

5 * 1=5
5 * 2=10
5 * 3=15
5 * 4=20
5 * 5=25
5 * 6=30
5 * 7=35
5 * 8=40
5 * 9=45
5 * 10=50

Checked:

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

namespace ConsoleApplication1
{
public class Class1
{
static int a = 12345678;
static int b = 78901234;
public static int checkedclass()
{
int c = 0;
try
{
c = checked (a * b);
}
catch (System.OverflowException e)
{
Console.WriteLine(e.ToString ());
}
return c;
}
}
}

System.OverflowException: Arithmetic operation resulted in an overflow.


at ConsoleApplication1.Class1.checkedclass() in D:\adva_oop\checkprogram\chec
kprogram\Class1.cs:line 17
0

Unchecked:

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

namespace ConsoleApplication1
{
public class Class1
{
static int a = 12345678;
static int b = 78901234;
public static int checkedclass()
{
int c = 0;
try
{
c = unchecked (a * b);
}
catch (System.OverflowException e)
{
Console.WriteLine(e.ToString ());
}
return c;
}
}
}

-764031556

Out Keyword:

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int c,d;
Class1.outclass(out c, out d);
Console .WriteLine (c);
Console.WriteLine(d);
Console.ReadLine();

}
}
}

12
13

Param Keyword:

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

namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
param p = new param();
p.paramclass(1, 2, 3, 4);
Console.WriteLine(sizeof(short));
Console.ReadLine();
}
}
}

1
2
3
4
2

Ref Keyword:

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

namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int a = 21;
Class1.printnew(ref a);
Console.WriteLine(a);
Console.ReadLine();
}

}
}

10

Sealed Program:

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

namespace ConsoleApplication1
{
sealed class sealedclass
{
public void add(int a, int b)
{
Console .WriteLine (a+b);
}
}

class Class1 : sealedclass


{
public void sub(int a, int b)
{
Console.WriteLine(a - b);
}
}

class Program
{
static void Main(string[] args)
{

}
}
}

Error:
'ConsoleApplication1.Class1': cannot derive from sealed type 'ConsoleApplication1.sealedclass'

As Keyword:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
object[] myobject = new object[4];
myobject[0] = "hello";
myobject[1] = 42655;
myobject[2] = "Zainab";
myobject[3] = null;
for(int i=0;i<=myobject.Length ;++i)
{
string s = myobject [i] as string ;
if(s != null)
Console.WriteLine(s);
else
Console .WriteLine ("Not a string");
}
Console.ReadLine();
}
}
}

hello
Not a string
Zainab
Not a string

Const Keyword with bool datatype:

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

namespace ConsoleApplication1
{

class Program
{
static void Main(string[] args)
{
const int a = 8;
Console.WriteLine("a=", Console.ReadLine());
bool b = (a == 5);
Console.WriteLine(b);
Console.ReadLine();
}
}
}

a=3
False

Properties:
Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class student
{
private int id;
private string name;
private string address;

public int stdid


{
get
{
return id;

}
set
{
id = value;
}
}
public string stdname
{
get
{
return name;

}
set
{
name = value;
}
}
public string stdaddress
{
get
{
return address;

}
set
{
address = value;
}
}
}
}

Main Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
student std = new student();
std.stdid = 2;
std.stdname = "zainab";
std.stdaddress = "73567865";
Console.WriteLine(std.stdid);
Console.WriteLine(std.stdname);
Console.WriteLine(std.stdaddress);
Console.ReadLine();

}
}
}

2
zainab
73567865

Indexer:
Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Class1
{
private string[] data = new string[3];
public string this[int a]
{
get
{
return data[a];
}
set
{
data[a] = value;
}
}
}
}

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

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Class1 c = new Class1();
c[0] ="Aisha";
c[1] ="Salam";
c[2] = "Zainab";
Console.WriteLine(c[0]);
Console.WriteLine(c[1]);
Console.WriteLine(c[2]);
Console.ReadLine();
}
}
}

Aisha
Salma
Zainab

Protected Keyword:

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

namespace ConsoleApplication1
{
class A
{
protected int a;
private int b;
}
class B : A
{
public B()
{
Console.WriteLine(this.a);
}
}
class Program
{
static void Main(string[] args)
{

B b = new B();
Console.ReadLine();

}
}
}

You might also like