C# programs

15
C# PROGRAMMING By Prof. A. Syed Mustafa

description

C# programs

Transcript of C# programs

Page 1: C# programs

C# PROGRAMMING By Prof. A. Syed Mustafa

Page 2: C# programs

How to execute a C# program?

Install setup package named dotNetFx40_Full_setup.exe can be

downloaded from Microsoft’s general download area

http://www.microsoft.com/en-us/download/details.aspx?id=17851

Set Path varable through Mycomputer’s property –Environment Variable

PATH: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319

Use Command prompt and then type the following command for compilation :

c:\mydir\csc programname.cs

To run the C# program

c:\mydir\programname Prof. Syed Mustafa(Ph.D) 2

Page 3: C# programs

Program to add 2 numbers

// type the program in notepad & save the program as “add.cs”

using System;

class add

{

public static void Main()

{

Console.WriteLine("Enter 2 numbers");

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

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

int c=a+b;

Console.WriteLine("sum of "+a+" and "+b+" is "+c);

}

}

Prof. Syed Mustafa(Ph.D) 3

Page 4: C# programs

Program to display Command line arguments

// type the program in notepad & save the program as “cmd.cs”

using System;

class cmd

{

public static void Main(string[] s )

{

Console.WriteLine("command line arguments");

for(int i=0;i<s.Length;i++)

Console.WriteLine(s[i]);

}

}

Prof. Syed Mustafa(Ph.D) 4

Page 5: C# programs

Program to display Command line arguments

// type the program in notepad & save the program as “cmd.cs”

using System;

class cmd

{

public static void Main(string[] s )

{

Console.WriteLine("command line arguments");

foreach(string i in s)

Console.WriteLine(i);

}

}

Prof. Syed Mustafa(Ph.D) 5

Page 6: C# programs

Program to display Command line arguments

// type the program in notepad & save the program as “cmd.cs”

using System;

class cmd

{

public static void Main(string[] s )

{

string[] a=Environment.GetCommandLineArgs();

Console.WriteLine("command line arguments");

for(int i=1;i<a.Length;i++) //s[0] is cmd.cs, here s[1] is first argument

Console.WriteLine(a[i]);

}

} Prof. Syed Mustafa(Ph.D) 6

Page 7: C# programs

Program to use Math class

using System; class sqt { public static void Main() { Console.WriteLine("Enter number"); int n=int.Parse(Console.ReadLine()); double r=Math.Sqrt(n); Console.WriteLine("square root of "+n+" is "+r); } }

Prof. Syed Mustafa(Ph.D) 7

Page 8: C# programs

Program to use Exception

using System; class Except1 { public static void Main() { try{ int a=5,b=0; Console.Write(“Multiple Exception"); int c=a/b; Console.WriteLine("C="+c); }catch(DivideByZeroException e) { Console.WriteLine("Divide by 0 Error"); }catch(ArithmeticException ae) { Console.WriteLine("Arithmetic Error"); } } }

Prof. Syed Mustafa(Ph.D) 8

Page 9: C# programs

Program to use Exception using System; class Except1 { public static void Main() { try{ int a=5,b=0; Console.Write(“Multiple Exception"); int c=a/b; Console.WriteLine("C="+c); }catch(DivideByZeroException e) { Console.WriteLine("Divide by 0 Error"); }catch(ArithmeticException ae) { Console.WriteLine("Arithmetic Error"); } finally { Console.WriteLine("From finally..."); } } }

Prof. Syed Mustafa(Ph.D) 9

Page 10: C# programs

Program to use Exception using System;

class Except3

{

public static void Main()

{

try{

int age=14;

if (age<15) throw new ArithmeticException();

Console.WriteLine("Age=" + age);

}catch(ArithmeticException ae)

{

Console.WriteLine("Age Error...");

}finally

{

Console.WriteLine("From finally...");

}

}

}

Prof. Syed Mustafa(Ph.D) 10

Page 11: C# programs

Program to use Exception using System;

class Except4

{

public static void Main()

{

int a=893434,b=78888;

try{

int c=checked(a*b);

Console.WriteLine("a=" + a);

}catch(OverflowException ae)

{

Console.WriteLine("Overflow Error...“+ae.Message);

Console.WriteLine(“Source Error...“+ae.Source);

}finally

{

Console.WriteLine("From finally...");

}

}

}

Prof. Syed Mustafa(Ph.D) 11

Page 12: C# programs

Program to use Exception

using System;

class Except

{

public static void Main()

{

try{

int a=5,b=0;

Console.WriteLine(“Exception");

int c=a/b;

Console.WriteLine("C="+c);

}catch(DivideByZeroException e)

{

Console.WriteLine("Divide by 0 Error: "+e);

Console.WriteLine("Error Message is: "+e.Message);

Console.WriteLine("Stack trace is: "+e.StackTrace);

Console.WriteLine("Error Source is: "+e.Source);

Console.WriteLine("Target Site is: "+e.TargetSite);

Console.WriteLine("HelpLink is: "+e.HelpLink);

}

}

}

Prof. Syed Mustafa(Ph.D) 12

Page 13: C# programs

Program to use customized Exception using System;

class AgeException:Exception

{public AgeException(String msg):base(msg){ }

}

class Except4custom

{public static void Main()

{ int age=4;

try{

if(age<15) throw new AgeException("Under Age");

Console.WriteLine("age=" + age);

}catch(AgeException ae)

{

Console.WriteLine("Error is...:"+ae);

}finally

{

Console.WriteLine("From finally...");

}

}

}

Prof. Syed Mustafa(Ph.D) 13

Page 14: C# programs

Program to use customized Exception using System;

class AgeException:Exception

{String m;

public AgeException(String msg)

{m=msg;

}

public override string Message

{

get { return m; }

}

}

class Except4custom2

{public static void Main()

{ int age=4;

try{

if(age<15) throw new AgeException("Under Age");

Console.WriteLine("age=" + age);

}catch(AgeException ae)

{

Console.WriteLine("Error is...:"+ae.Message);

}

}

} Prof. Syed Mustafa(Ph.D) 14

Page 15: C# programs

Program to use customized Exception using System;

class AgeException:Exception

{ String m;

public AgeException(String msg)

{m=msg;

}

public override string Message

{

get { m+="Age is not valid"; return m; }

}

}

class Except4custom1

{ public static void Main()

{ int age=4;

try{

if(age<15) throw new AgeException("Under Age");

Console.WriteLine("age=" + age);

}catch(AgeException ae)

{

Console.WriteLine("Error is...:"+ae.Message);

}

}

} Prof. Syed Mustafa(Ph.D) 15