Overview of C-Sharp.pptx

download Overview of C-Sharp.pptx

of 44

Transcript of Overview of C-Sharp.pptx

  • 7/25/2019 Overview of C-Sharp.pptx

    1/44

    Overview of C#Why the name C# ???

    C# seems a strange name for a modernprogramming language.

    Microsoft named this language as c-sharpbecause they wanted it to be better,smarter and sharper than its ancestors cand c++

  • 7/25/2019 Overview of C-Sharp.pptx

    2/44

    Overview of C#

    C# is the only programminglanguage designed specially forthe .N! platform which pro"idestools and ser"ices to fully eploitboth communications andcomputing.

  • 7/25/2019 Overview of C-Sharp.pptx

    3/44

    Overview of C#C# can be used to

    de"elop two categories ofapplications$programs

    a. ecutable applicationprograms and

    b. Component libraries

  • 7/25/2019 Overview of C-Sharp.pptx

    4/44

    Overview of C#

    %imple C# program

    !he best way to learn c# programis write simple programs and

    eecute them.

  • 7/25/2019 Overview of C-Sharp.pptx

    5/44

    Overview of C#Class &eclaration

    !he 'rst line class %ample(ne declares a class,

    which is an ob)ect-oriented construct.

    C# is truly ob)ect oriented language, thereforee"ery thing must be placed inside the class.

    Class is a *eyword that declares new class.

    %ample(ne is C# identi'er that speci'es name ofthe class.

  • 7/25/2019 Overview of C-Sharp.pptx

    6/44

    Overview of C#races

    C# is a bloc* structured language,meaning code bloc*s are always

    enclosed by braces and

    !herefore, e"ery class de'nition begins

    with opening brace and ends withclosing brace.

  • 7/25/2019 Overview of C-Sharp.pptx

    7/44

    Overview of C#!he Main Method

    !he third line

    public static "oid Main/ de'nes a methodnamed Main. !his is the starting point foreecuting the program.

    0 C# program can ha"e many classes, butonly one class can Main method to initiateeecution.

  • 7/25/2019 Overview of C-Sharp.pptx

    8/44

    Overview of C#!he *eyword public is an access modi'er thattells the compiler that Main method is accessible

    by e"ery one.

    !he *eyword static declares that Main method isglobal and can be called without creating aninstance of the class.

    !he *eyword "oid is a type modi'er that statesthat Main method does not return any "alue.

  • 7/25/2019 Overview of C-Sharp.pptx

    9/44

    Overview of C#!he (utput 1ine

    !he only eecutable statement in C# program is

    %ystem.Console.Write1ine2C# is sharper thanC#/3

    !his is similar to 4a"a output statement orprintf/ from c and cout from c++

  • 7/25/2019 Overview of C-Sharp.pptx

    10/44

    Overview of C#!he Write1ine method is a static method ofConsole Class which is located in namespace

    %ystem.

    !he method Write1ine always appends anew-line character at the end of the string.

    "ery C# statement should end with a semicolon.

  • 7/25/2019 Overview of C-Sharp.pptx

    11/44

    Overview of C#!he Write1ine method is a static method ofConsole Class which is located in namespace

    %ystem.

    !he method Write1ine always appends a new-linecharacter at the end of the string.

    "ery C# statement should end with a semicolon. Note that at the end of class semicolon isnot re5uired in C#.

  • 7/25/2019 Overview of C-Sharp.pptx

    12/44

    Overview of C#ecuting the program

    0fter creating your program i.e. source code, sa"e the

    program with .cs etension in one of your folders insystem. Choose any suitable name for sa"ingprogram for eample, 2programone.cs6

    7or compiling your program go to the folder where theprogram is sa"ed then type the following command8

    csc programone.cs 9command prompt/

  • 7/25/2019 Overview of C-Sharp.pptx

    13/44

    Overview of C#!he C# Compiler C%C/compiles your program and

    creates an eecutable 'le:1 code/ by namesampleone.ee in the samedirectory

    7or eecuting the program,simply type in name of theeecutable 'le at prompt.

  • 7/25/2019 Overview of C-Sharp.pptx

    14/44

    Overview of C#Namespaces

    Consider the statement%ystem.Console.Write1ine/3

    :n this statement %ystem is namespacescope/ in whichConsole class is located. 0 class in namespace can beaccessed using dot operator as shown abo"e.

    Namespaces are the ways C# segregates the .N!1ibrary classes into groupings.

  • 7/25/2019 Overview of C-Sharp.pptx

    15/44

    Overview of C#C# supports a feature *nown as usingdirecti"e to import namespace foreample %ystem into program. (nce

    namespace is imported, we can usethe elements of that namespacewithout using the namespace aspre'.

  • 7/25/2019 Overview of C-Sharp.pptx

    16/44

    Overview of C#0dding Comments

    Comments play an important role inmaintenance of programs. !hey are used toenhance readability and understanding code.

    C# permits two types of comments

    a. %ingle-line Comments

    b. Multiline Comments

  • 7/25/2019 Overview of C-Sharp.pptx

    17/44

    Overview of C#%ingle line comments begins with doublebac*slash character $$/ and terminate at the end

    of the line.

    :f you want to use multiple lines for a comment,we must use second type of comment *nown as

    multiline comment

    !he comment starts with $; and terminates with;$

  • 7/25/2019 Overview of C-Sharp.pptx

    18/44

    Overview of C#8 $$using namespace3

    $; using namesapce3 ;$

    Main returning a value

    !his is another aspect of Main Method.

  • 7/25/2019 Overview of C-Sharp.pptx

    19/44

    Overview of C#When the return type is int, we must declare

    a return statement at the end of the method.

    8 using %ystem3

    class %amplethree

    public static int Main/ Console.Write1ine2=ello6/3

    return >3

  • 7/25/2019 Overview of C-Sharp.pptx

    20/44

    Overview of C#Using aliases for Namespace Class

    Can we a"oid the pre' of %ystem.Console tosome of the methods of Console Class???

    !he answer is yes. %ince %ystem is anamespace and Console is class, the usingdirecti"e can be applied to only namespacesand cannot be applied to classes.

  • 7/25/2019 Overview of C-Sharp.pptx

    21/44

    Overview of C#

    sing %ystem.Console $$illegal use in C#

    We can o"ercome this problem by using aliases for

    namespace classes.

    !his ta*es the form alias-name@classname3

    sing 0@%ystem.Console3

    Class sample7our

    Aublic static "oid Main/

    0.Write1ine2=ello6/3

  • 7/25/2019 Overview of C-Sharp.pptx

    22/44

    Overview of C#Passing String objects to WriteLine metho

    We ha"e seen passing only constant string output to theConsole. We can store string "alues in string ob)ects and usethese ob)ects as parameters to the Write1ine method.

    8 string s@2abc63

    !he content of s can be printed out using Write1ine method.

    %ystem.Console.Write1ines/3

  • 7/25/2019 Overview of C-Sharp.pptx

    23/44

    Overview of C#Command 1ine 0rguments

    !here may be occasions where program need tobeha"e in a particular way depending on inputpro"ided at the time of eecution.

    !his is achie"ed in the C# by using what are *nown as

    Command line arguments.

    Command line arguments parameters supplied toMain method at time of in"o*ing it for eecution.

  • 7/25/2019 Overview of C-Sharp.pptx

    24/44

    Overview of C#8 using %ystem3

    class %amplesi

    public static "oid MainstringB args/

    Console.Write2Welcome to6/3

    Console.Write2 2+argsB>/3

    Console.Write2 2+argsBD/3

  • 7/25/2019 Overview of C-Sharp.pptx

    25/44

    Overview of C#:n the earlier eamples, we ha"e used Mainmethod with no parameters. Now in this

    eample, you can see Main is declared with aparameter args. !he parameter args isdeclared as an array of strings*nown as stringob)ects/

    0ny arguments pro"ided in the command lineat the time of eecution/ are passed to thearray args as its elements.

  • 7/25/2019 Overview of C-Sharp.pptx

    26/44

    Overview of C#We can access the array elements by usinga subscript li*e argsB>, argsBD and so on.

    7or eample if compile the abo"e saidprogram as

    %amplesi C %harp, !he command linecontains two arguments which are assignedto the args array as follows8

    argsB>-E C and argsBD-E %harp

  • 7/25/2019 Overview of C-Sharp.pptx

    27/44

    Overview of C#Main with a Class

    Consider a program with two classes butonly one Main function

  • 7/25/2019 Overview of C-Sharp.pptx

    28/44

    Overview of C#!his program has two class declarations one for the!estClass and another for Main method. !estClass

    contains only one method to print a string 2C# ismodern6

    !he Main method in %amplese"en class creates anob)ect of !estClass and uses it to in"o*e the method

    fun/ contained in !estClass

    !estClass test@new !estClass/3

    test.fun/3

  • 7/25/2019 Overview of C-Sharp.pptx

    29/44

    Overview of C#Proviing !nteractive !nput

    %o far we ha"e seen two approaches for "aluesto string ob)ects8

    a. sing an assignment statement

    b. !hrough command line arguments

    :t is also possible to gi"e "alues to string ob)ectsthrough *eyboard at the time of eecution.

  • 7/25/2019 Overview of C-Sharp.pptx

    30/44

    Overview of C#

    Console.Write2nter your name86/3

    (utputs the message 2nter your name86Console.Fead1ine/3

    causes the eecution to wait for the user toenter his name.

  • 7/25/2019 Overview of C-Sharp.pptx

    31/44

    Overview of C#

  • 7/25/2019 Overview of C-Sharp.pptx

    32/44

    Overview of C#sing Mathematical functions

    0ssume you would li*e to compute and

    print s5uare root of a gi"en number, it canbe accomplished through this program8

  • 7/25/2019 Overview of C-Sharp.pptx

    33/44

    Overview of C#!he abo"e program contains of eecutablestatements li*e8

    double @G.>3

    double y3

    y@Math.%5rt/3

  • 7/25/2019 Overview of C-Sharp.pptx

    34/44

    Overview of C#Multiple Main Methods

    :t is stated earlier that one of the classes mustha"e Main method as a member which is normalin eecution.

    C# includes a feature that enables us to de'nemore than one class with Main method, sinceMain is entry point for program eecution, sothere are more than one entry point.

  • 7/25/2019 Overview of C-Sharp.pptx

    35/44

    Overview of C#

  • 7/25/2019 Overview of C-Sharp.pptx

    36/44

    Overview of C#Multiple Main Methods

    :t is stated earlier that one of the classes mustha"e Main method as a member which is normalin eecution.

    C# includes a feature that enables us to de'nemore than one class with Main method, sinceMain is entry point for program eecution, sothere are more than one entry point.

  • 7/25/2019 Overview of C-Sharp.pptx

    37/44

    Overview of C#!he problem of resol"ing ha"ing more thanone entry point i.e. Main will be specifying

    which Main to be used at compile time by thecompiler.

    csc 'lename.cs $main8classname

    7or eample csc multimain.cs$main8class 0 or

    csc multimain.cs$main8class

  • 7/25/2019 Overview of C-Sharp.pptx

    38/44

    Overview of C#Compile !ime rrors

    Feal life applications contain a large number ofstatements with comple logic. No matter how thedesign is carried out, and no matter how much care ista*en during coding, we cannot say that total programis error-free.

    !here are two types of errors

    a. %ynta errors.

    b. 1ogic errors.

  • 7/25/2019 Overview of C-Sharp.pptx

    39/44

    Overview of C#%ynta errors are caught by the compiler,logic errors can be eliminated by programlogic carefully.

  • 7/25/2019 Overview of C-Sharp.pptx

    40/44

    Overview of C#When the abo"e program is complied, some errorsare listed down. %ince the compiler could not

    locate namespace %ystom, the followinginformation along with errors are generated.

    a. Name of the 'le being compiled.

    b. 1ine number and column position of the error.c. rror code de'ned by the compiler

    d. %hort description of the error.

  • 7/25/2019 Overview of C-Sharp.pptx

    41/44

    Overview of C#Arogram %tructure

    0n eecutable C# program may contain

    Number of coding bloc*s

    !he documentation section contains

    0 set of comments gi"ing the name ofthe program, the author date and other

    details.

  • 7/25/2019 Overview of C-Sharp.pptx

    42/44

    Overview of C#!he using directi"e section will include allthose namespaces that contain classes

    re5uired by the application. sing directi"etells the compiler to loo* in the namespacespeci'ed for those unresol"ed classes.

    0n interface is similar to class but onlycontain abstract members. :t is used toimplement the concept of inheritance.

  • 7/25/2019 Overview of C-Sharp.pptx

    43/44

    Overview of C#0 c# program can contain multiple classde'nitions. Classes are essential and primary

    elements of c# programs. !he classes areused to map ob)ects of real-world problems.

    %ince e"ery c# application program re5uires

    Main method as its starting point the classcontaining Main is essential part of theprogram.

  • 7/25/2019 Overview of C-Sharp.pptx

    44/44

    Overview of C#

    Arogramming coding style

    C# is a freeform language. We need not to intend anylines to ma*e the program wor* properly.

    C# does not care where on the line we begin coding.%e"eral alternati"e styles are possible we should useone from among them consistently.