SynapseIndia Fundamentals of Dotnet Development Plateforms

Embed Size (px)

Citation preview

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    1/50

    1

    SynapseI ndia Fundamentalsof Dotnet

    Development Plateforms

    C# is a language that was developed by Microsoft

    specifically targeted for the .NET platform.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    2/50

    2

    C# Features

    No pointers

    Automatic memory management

    Completely object-oriented

    Supports interface-based programming

    Both implementation inheritance and interfaceinheritance supported

    Support for overloaded operators

    Support for aspect-based (attribute-based)programming

    Can only produce managed-code

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    3/50

    3

    Lets try a simple program.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    4/50

    4

    Create a Working Directory

    mkdir k:\week1\hello

    Copy corvars.bat

    copy

    c:\Program Files\Microsoft Visual Studio.NET

    \FrameworkSDK\bin\corvars.bat

    k:\week1\hello\corvars.bat

    Set up environment, run covars.bat

    Cd k:\week1\hello

    Type corvars.bat

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    5/50

    5

    Open notepad, create the following fi le and

    save it in the k:\week1\hel lo directory ashello.cs.

    class Hello

    {

    static void Main()

    {

    System.Console.WriteLine("Hello World");

    }

    }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    6/50

    6

    Compile the program

    k:\week1\hello>csc Hello.cs

    Run the programK:\week1\hello>Hello.exe

    K:\week1\hello> Hello World

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    7/50

    7

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    8/50

    8

    C#.NET Language Basics

    Types in C#

    Defining integer types

    A Bit About Strings

    Reading From and Writing To The Console

    If Then Statement

    LoopingThe For Next Statement

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    9/50

    9

    Primitive Types

    C# Type .NET Framework type

    bool System.Boolean

    byte System.Byte

    sbyte System.Sbyte

    char System.Char

    decimal System.Decimal

    double System.Double

    float System.Single

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    10/50

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    11/50

    11

    A word on types

    All types in .NET derive from System.Object

    They are provided implementations of ToString() and

    GetType() To get a string with the type of any variable, you can

    call .GetType()

    Whenever you call Console.WriteLine(obj) the

    ToString() method on objis implicitly called. Thedefault ToString implementation for classes simplyreturns the name of the class.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    12/50

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    13/50

    13

    Defining Integers

    int i;

    int i, j, k;

    int i = 12;

    j = i;

    j is now equal to 12i = 15;

    k = i + j; k is equal to 27

    To write an Integer, convert it to a String using:

    k.ToString();

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    14/50

    14

    A Bit About Str ings

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    15/50

    15

    What are str ings?

    abcdef Abcdef aBcdEf A23+-/*789 qJohn J. Smith

    How do you do?

    123 South Street, Calais, ME 04235 Are we there?

    an empty string

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    16/50

    16

    How do we define str ings?

    string strTmp;

    strTmp = time will tell;

    string strTmp = time will tell;

    strTmp = Console.ReadLine();

    string strTmp2;

    strTmp2 = strTmp;

    strTmp2time will tell

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    17/50

    17

    Concatenating Str ings

    string strCity = Calais;

    string strState = ME;

    string strZip = 04270;string strLoc;

    strLoc = strCity + , + strState + + strZip;

    strLocCalais, ME 04270

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    18/50

    18

    Some Str ing Functions

    string strTmp;

    strTmp.Trim();removes leading and trailing spaces

    strTmp.ToUpper();converts string to all upper case

    strTmp.ToLower();converts string to all lower case

    strTmp.Length;returns string length as an integer

    strTmp.SubString()extracts a substring

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    19/50

    19

    Str ing Function Examples

    string strTmp = Hello World ;

    strTmp.Trim();

    strTmp

    Hello World

    string strTmp = Hello World;

    strTmp.ToLower(); hello world

    strTmp.ToUpper(); HELLO WORLD

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    20/50

    20

    String.Length Function

    string strTmp;

    strTmp = in the beginning;

    The value of strTmp.Length is 16.int i;

    i = strTmp.Length;

    The value of i is 16.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    21/50

    21

    Str ing.SubStr ing() Function

    String.Substring(startIndex , length );

    Parameters (are Integers)

    startIndexWhere the substring starts.startIndex is zero-based.

    lengthThe number of characters in the substring.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    22/50

    22

    Substr ing Examples

    string strTmp;

    strTmp = around the world;

    strTmp.Substring(0,6);

    aroundstrTmp.Substring(11,5);world

    strTmp.Substring(0,strTmp.Length);

    around the world

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    23/50

    23

    Writing to the Console

    Console.WriteLine(String); write with line return

    Console.WriteLine(Hi There)

    C:\>Hi There

    C:\>

    Console.Write(String);write with no line return

    Console.Write(Hi There)C:\>Hi There

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    24/50

    24

    Reading from the Console

    Console.ReadLine();returns a string

    string tmp;

    Console.Write(What is your name? );

    tmp = Console.ReadLine();

    Console.WriteLine(Hi + tmp);

    C:\>What is your name? ChipC:\>Hi Chip

    C:\>

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    25/50

    25

    i f Statement

    if (some condition is true)

    {

    do something in here,

    using one or more lines of code

    }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    26/50

    26

    What is difference between = and ==?

    = is for assignment of value

    String tmpString = Hello world;

    int i = 12;

    == is for equivalence

    if (str1 == str2) { some code }if (str.Length == 0) { some code }

    if (str1 != end) { some code }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    27/50

    27

    Sample if Statement

    string strInput ;

    strInput = Console.ReadLine();

    if (strInput == )

    {

    Console.WriteLine(Input required.);

    }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    28/50

    28

    The For Loop

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    29/50

    29

    A Simple For Loop

    int i;for (i = 1; i

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    30/50

    30

    Or You Could Reverse It

    int i;for (i = 10; i>0; i--)

    {

    Console.WriteLine("The value of i is " + i.ToString());

    }

    The Value of i is 10

    The Value of i is 9

    The Value of i is 8

    The Value of i is 2

    The Value of i is 1

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    31/50

    31

    To Walk Through a Str ing

    string tmp = hello world;

    for (int k = 0; k< tmp.Length-1;k++)

    {

    Console.WriteLine(tmp.Substring(k,1));

    }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    32/50

    32

    To Walk Through a Str ing Backward

    string tmp = "hello world";

    for (int k =tmp.Length-1;k>-1;k--)

    {

    Console.WriteLine(tmp.Substring(k,1));

    }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    33/50

    33

    What About?

    What If We Want To Enter More Data?

    What If No String Is Entered?What If The Entered String Is Too Long?

    How Do We Know When We Are Done?

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    34/50

    34

    What I f We Want To Enter More Data?

    LabelsA Label is Defined with a Colon ReturnHere:

    goto Statements

    goto Statements Direct Program Flow To A Label

    goto ReturnHere;

    GoTo Statements are Evil and High Risk!!!

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    35/50

    35

    What I f No Str ing I s Entered?

    Checking for a zero length string.

    if (tmpStr.Length == 0)

    {

    Console.WriteLine(No String Entered);

    goto ReturnHere;

    }

    Note: You could also check for tmpStr ==

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    36/50

    36

    What I f The Entered Str ing I s Too Long?

    Lets only work with strings up to 10 characters

    if (strTmp.Length > 10)

    {

    strTmp = strTmp.SubString(0,10);

    }

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    37/50

    37

    How Do We Know When We Are Done?

    Lets check for the string end to end the program

    if (strTmp == end)

    {

    return;}

    Note: return tells the program to exit the subroutine, which in this

    case will end the program.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    38/50

    38

    Comments in C#

    Both /* */ and // can be used for comments.

    VS provides comment/uncomment selections.

    Use the menu bar, or Ctrl-K Ctrl-C for comment and Ctrl-K Ctrl-U for uncomment

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    39/50

    39

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    40/50

    40

    Now lets redo hello.cs as a

    Visual Studio project.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    41/50

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    42/50

    42

    To get Visual Studio.NET

    You need to purchase either:

    An MSDN subscription

    A copy of Visual Studio.NET Academic editions are available (in or through the

    bookstore ?)

    Vi l St di NET

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    43/50

    43

    Visual Studio.NET

    Start up Visual Studio.NET

    Open a new project by either: Clicking on theNew Projectbutton on the Start Page

    OR

    File-> New-> Project from the Menu Bar

    In Visual C# Projects, create a ConsoleApplication

    Implement the Main() method

    Notice you now have IntelliSense

    Add the Console.WriteLine line of code.

    Compile using the Build menu.

    Run using the Debug menu

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    44/50

    44

    Your code should look like this

    using System;

    namespace HelloVS{//////Summary description for App.///class App{

    //////The main entry point for the application.///[STAThread]static void Main(string[] args)

    {System.Console.WriteLine("Hello World");

    }}}

    Cl E i (ti i tti )

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    45/50

    45

    Class Exercise (time permitting)

    Using the Visual Studio.NET write an interactive console

    program to accept information from the keyboard and

    then format and display the information back. It might be

    a persons name and address or a variable list of favorite

    pets including name and type of animal or whatever.Focus on formatting the data, looping to accept multiple

    entries, testing for missing information and also testing

    for an at end condition. A sample, somewhat simplified

    example is in the Class Collections zip file on the web(i.e. www.PondviewSoftware.com).

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    46/50

    46

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    47/50

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    48/50

    48

    Homework Part 2

    C#.Net Programming

    Homework AssignmentWeek 1

    Assignment Due: November 13, 2003 5:30 PM

    Write a C# NET Console Application which

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    49/50

    49

    Write a C#.NET Console Application, which

    performs the fol lowing:1. Accepts a first name string, a middle name string, and a last name string from

    the console. The first name and last name are required. The middle name isoptional.

    2. Concatenates the two or three fields together creating a full name string.

    3. Truncates the full name to 20 characters if the length of the full name islonger than 20 characters.

    4. Provides the capability of displaying the full name either vertically orhorizontally and forward or backward as desired.

    5. Allows the full name field to be displayed in either of these four ways asmany times as desired.

    6. Allows the user to go back to the top and start over, entering a new name.

    7. Terminates gracefully.

    Note:

    There was enough information discussed in todays class to complete thisassignment. Feel free to use any additional commands, structures, orfunctions you wish.

  • 8/10/2019 SynapseIndia Fundamentals of Dotnet Development Plateforms

    50/50

    50