buildingc#application

Upload: sujala-v-koparde

Post on 14-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 BuildingC#Application

    1/12

    C# PROGRAMMING WITH .NET3.0

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    2/12

    Building C# Applications Using csc.exe

    Open Notepad and enter the following:

    using System;

    class TestApp

    {

    public static void Main ()

    {

    Console.WriteLine ("Testing! 1, 2, 3");

    }

    }

    Save the file in a convenient location (e.g., C:\CscExample) as TestApp.cs

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    3/12

    The different options of c# compiler1)/out

    This option is used to specify the name of the assembly to be created

    By default, the assembly name is the same as the name of the initial input *.cs file(in the case of a *.dll) or the name of the type containing the programs Main()method (in the case of an *.exe)

    csc /out:My.exe File.cs

    2) /target:exe

    This option builds an executable console application

    This is the default file output type, and thus may be omitted when building thisapplication type

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    4/12

    3) /target:library

    This option builds a single-file *.dll assembly

    csc /target:library File.cs

    4) /target:module

    This option builds a module

    Modules are elements of multifile assemblies

    5)/target:winexe

    Although you are free to build Windows-based applications using the/target:exe flag, the /target:winexe flag prevents a console windowfrom appearing in the background

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    5/12

    Referencing External Assemblies

    using System;

    using System.Windows.Forms;

    class TestApp

    {

    public static void Main()

    {

    Console.WriteLine("Testing! 1, 2, 3");

    MessageBox.Show("Hello...");}

    }

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    6/12

    using System;

    using System.Windows.Forms;class HelloMessage

    {

    public void Speak()

    {

    MessageBox.Show("Hello...");}

    }

    Compiling Multiple Source Files

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    7/12

    Consider the following TestApp class which uses HelloMessage class:

    using System;

    class TestApp{

    public static void Main ()

    {

    Console.WriteLine("Testing! 1, 2, 3");

    HelloMessage h = new HelloMessage();h.Speak();

    }

    }

    csc /r:System.Windows.Forms.dll testapp.cs hellomsg.cs

    csc /r:System.Windows.Forms.dll *.cs

    By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    8/12

    C# Language Fundamentals

    C# demands that all program logic is contained within atype definition (class, interface, structure,enumeration,

    delegate The Anatomy of a Simple C# Program:

    //C# files end with a *.cs file extension.

    using System;

    class HelloClass{

    public static int Main(string[] args)

    {

    Console.WriteLine("Hello world");return 0;

    }

    }By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    9/12

    Variations on the Main ( ) Method//Integer return type, array of strings as argument.

    public static int Main(string[] args)

    {// Process command line arguments.

    // Make some objects.

    //Return a value to the system.

    }-------------------------------------------------------------------

    // No return type, no arguments.public static void Main()

    {

    // Make some objects.

    }-----------------------------------------------------------------

    // Integer return type, no arguments.public static int Main()

    {

    // Make some objects.

    // Return a value to the system.

    }By Miss S.V.Koparde

  • 7/30/2019 BuildingC#Application

    10/12

    Processing Command Line Parameters

    using System;

    classHelloClass{

    public static int Main(string[] args)

    {

    Console.WriteLine("***** Command line args *****");for(int x = 0; x

  • 7/30/2019 BuildingC#Application

    11/12

    Alternative to the standard for loop C# "foreach"keyword.

    // Notice we have no need to check the size of thearray when using

    //'foreach'.

    public static int Main(string[] args){

    foreach(string s in args)

    Console.WriteLine("Arg: {0} ", s);

    ...

    }

    By Miss S.V.Koparde

    h l

  • 7/30/2019 BuildingC#Application

    12/12

    The System.Environment Classusing System;

    class Exp

    {

    public static void Main()

    {

    // OS running this app?

    Console.WriteLine("Current OS: {0} ", Environment.OSVersion);

    // Directory containing this app?

    Console.WriteLine("Current Directory:{0}",Environment.CurrentDirectory);

    // List the drives on this machine.

    string[] drives = Environment.GetLogicalDrives();

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

    Console.WriteLine("Drive {0} : {1} ", i, drives[i]);

    // Which version of the .NET platform is running this app?

    Console.WriteLine("Executing version of .NET: {0}",Environment.Version);

    }

    } By Miss S.V.Koparde