SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
C# PROGRAMMING
       By

Prof. A. Syed Mustafa
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:WINDOWSMicrosoft.NETFrameworkv4.0.30319

Use Command prompt and then type the following command for
  compilation :
c:mydircsc programname.cs

To run the C# program
c:mydirprogramname
                                               Prof. Syed Mustafa(Ph.D)   2
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
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
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
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
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
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
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
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
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
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
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
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
}
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
}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Types of function call
Types of function callTypes of function call
Types of function call
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function
FunctionFunction
Function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Function lecture
Function lectureFunction lecture
Function lecture
 
C functions
C functionsC functions
C functions
 
Function in c
Function in cFunction in c
Function in c
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 

Andere mochten auch

Setting up a gmail account
Setting up a gmail accountSetting up a gmail account
Setting up a gmail accountkeelyswitzer
 
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water ResourcesWE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resourcesgrssieee
 
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceGuidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceeWater
 
Appendex g
Appendex gAppendex g
Appendex gswavicky
 
Offshore pipelines
Offshore pipelinesOffshore pipelines
Offshore pipelineshaiifa25
 
Chapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power SuppliesChapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power SuppliesPatty Ramsey
 
Survey Grade LiDAR Technologies for Transportation Engineering
Survey Grade LiDAR Technologies for Transportation EngineeringSurvey Grade LiDAR Technologies for Transportation Engineering
Survey Grade LiDAR Technologies for Transportation EngineeringQuantum Spatial
 
Chapter 4 Form Factors Power Supplies
Chapter 4 Form Factors Power SuppliesChapter 4 Form Factors Power Supplies
Chapter 4 Form Factors Power SuppliesPatty Ramsey
 
Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Chhom Karath
 
Chapter 10 Synchronous Communication
Chapter 10 Synchronous CommunicationChapter 10 Synchronous Communication
Chapter 10 Synchronous CommunicationPatty Ramsey
 
Appendex f
Appendex fAppendex f
Appendex fswavicky
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPprabhatjon
 
Appendex e
Appendex eAppendex e
Appendex eswavicky
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionMazenetsolution
 
Appendex a
Appendex aAppendex a
Appendex aswavicky
 
Preparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability Extension
Preparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability ExtensionPreparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability Extension
Preparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability ExtensionSafe Software
 
PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3melechi
 

Andere mochten auch (20)

Setting up a gmail account
Setting up a gmail accountSetting up a gmail account
Setting up a gmail account
 
Chapter 5 Input
Chapter 5 InputChapter 5 Input
Chapter 5 Input
 
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water ResourcesWE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
 
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceGuidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
 
Appendex g
Appendex gAppendex g
Appendex g
 
Offshore pipelines
Offshore pipelinesOffshore pipelines
Offshore pipelines
 
Chapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power SuppliesChapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power Supplies
 
Survey Grade LiDAR Technologies for Transportation Engineering
Survey Grade LiDAR Technologies for Transportation EngineeringSurvey Grade LiDAR Technologies for Transportation Engineering
Survey Grade LiDAR Technologies for Transportation Engineering
 
Ch07
Ch07Ch07
Ch07
 
Chapter 4 Form Factors Power Supplies
Chapter 4 Form Factors Power SuppliesChapter 4 Form Factors Power Supplies
Chapter 4 Form Factors Power Supplies
 
Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)
 
Chapter 10 Synchronous Communication
Chapter 10 Synchronous CommunicationChapter 10 Synchronous Communication
Chapter 10 Synchronous Communication
 
Appendex f
Appendex fAppendex f
Appendex f
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP - Introduction to PHP Date and Time Functions
PHP -  Introduction to  PHP Date and Time FunctionsPHP -  Introduction to  PHP Date and Time Functions
PHP - Introduction to PHP Date and Time Functions
 
Appendex e
Appendex eAppendex e
Appendex e
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
 
Appendex a
Appendex aAppendex a
Appendex a
 
Preparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability Extension
Preparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability ExtensionPreparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability Extension
Preparing LiDAR for Use in ArcGIS 10.1 with the Data Interoperability Extension
 
PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3
 

Ähnlich wie C# programs (20)

39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 
Java programs
Java programsJava programs
Java programs
 
C#
C#C#
C#
 
C#.net
C#.netC#.net
C#.net
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 
Dotnet 18
Dotnet 18Dotnet 18
Dotnet 18
 
Java practical
Java practicalJava practical
Java practical
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
 
C# Lab Programs.pdf
C# Lab Programs.pdfC# Lab Programs.pdf
C# Lab Programs.pdf
 
C# Lab Programs.pdf
C# Lab Programs.pdfC# Lab Programs.pdf
C# Lab Programs.pdf
 
Hems
HemsHems
Hems
 
.net progrmming part1
.net progrmming part1.net progrmming part1
.net progrmming part1
 
P2
P2P2
P2
 
C# programs
C# programsC# programs
C# programs
 
Anti patterns
Anti patternsAnti patterns
Anti patterns
 
Java file
Java fileJava file
Java file
 

Mehr von Syed Mustafa

BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfBPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfSyed Mustafa
 
18CSL58 DBMS LAB Manual.pdf
18CSL58 DBMS LAB Manual.pdf18CSL58 DBMS LAB Manual.pdf
18CSL58 DBMS LAB Manual.pdfSyed Mustafa
 
Syed IoT - module 5
Syed  IoT - module 5Syed  IoT - module 5
Syed IoT - module 5Syed Mustafa
 
15CS81- IoT- VTU- module 3
15CS81- IoT- VTU- module 315CS81- IoT- VTU- module 3
15CS81- IoT- VTU- module 3Syed Mustafa
 
15CS81- IoT Module-2
15CS81- IoT Module-215CS81- IoT Module-2
15CS81- IoT Module-2Syed Mustafa
 
Internet of Things - module 1
Internet of Things -  module 1Internet of Things -  module 1
Internet of Things - module 1Syed Mustafa
 
15CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 115CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 1Syed Mustafa
 
15CS664- Python Application Programming VTU syllabus
15CS664- Python Application Programming  VTU syllabus15CS664- Python Application Programming  VTU syllabus
15CS664- Python Application Programming VTU syllabusSyed Mustafa
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3Syed Mustafa
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4Syed Mustafa
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3Syed Mustafa
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
Grid computing notes
Grid computing notesGrid computing notes
Grid computing notesSyed Mustafa
 
Unix system programming
Unix system programmingUnix system programming
Unix system programmingSyed Mustafa
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdSyed Mustafa
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CSyed Mustafa
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programsSyed Mustafa
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversionSyed Mustafa
 

Mehr von Syed Mustafa (20)

BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfBPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
 
18CSL58 DBMS LAB Manual.pdf
18CSL58 DBMS LAB Manual.pdf18CSL58 DBMS LAB Manual.pdf
18CSL58 DBMS LAB Manual.pdf
 
Syed IoT - module 5
Syed  IoT - module 5Syed  IoT - module 5
Syed IoT - module 5
 
IoT - module 4
IoT - module 4IoT - module 4
IoT - module 4
 
15CS81- IoT- VTU- module 3
15CS81- IoT- VTU- module 315CS81- IoT- VTU- module 3
15CS81- IoT- VTU- module 3
 
15CS81- IoT Module-2
15CS81- IoT Module-215CS81- IoT Module-2
15CS81- IoT Module-2
 
Internet of Things - module 1
Internet of Things -  module 1Internet of Things -  module 1
Internet of Things - module 1
 
15CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 115CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 1
 
15CS664- Python Application Programming VTU syllabus
15CS664- Python Application Programming  VTU syllabus15CS664- Python Application Programming  VTU syllabus
15CS664- Python Application Programming VTU syllabus
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Usp notes unit6-8
Usp notes unit6-8Usp notes unit6-8
Usp notes unit6-8
 
Grid computing notes
Grid computing notesGrid computing notes
Grid computing notes
 
Unix system programming
Unix system programmingUnix system programming
Unix system programming
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programs
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 

C# programs

  • 1. C# PROGRAMMING By Prof. A. Syed Mustafa
  • 2. 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:WINDOWSMicrosoft.NETFrameworkv4.0.30319 Use Command prompt and then type the following command for compilation : c:mydircsc programname.cs To run the C# program c:mydirprogramname Prof. Syed Mustafa(Ph.D) 2
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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 }
  • 15. 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 }