Top Banner

of 50

Dot NET Fundamentals Part2

Jun 02, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 Dot NET Fundamentals Part2

    1/50

    1

    An introduction to C#

    C# is a language that was developed by Microsoft

    specifically targeted for the .NET platform.

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    3/50

    3

    Lets try a simple program.

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    7/50

    7

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    10/50

    10

    Primitive Types (contd.)

    int System.Int32

    uint System.UInt32

    long System.Int64

    ulong System.UInt64

    object System.Object

    short System.Int16

    ushort System.UInt16

    string System.String

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    12/50

    12

    What Are I ntegers

    0, 432, -5, 10000000, -10000000

    Integers are whole numbers

    Integervariables are stored as signed 32-bit

    (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647.

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    14/50

    14

    A Bit About Str ings

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    27/50

    27

    Sample if Statement

    string strInput ;

    strInput = Console.ReadLine();

    if (strInput == )

    {

    Console.WriteLine(Input required.);

    }

  • 8/10/2019 Dot NET Fundamentals Part2

    28/50

    28

    The For Loop

  • 8/10/2019 Dot NET Fundamentals Part2

    29/50

    29

    A Simple For Loop

    int i;for (i = 1; i

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    39/50

    39

  • 8/10/2019 Dot NET Fundamentals Part2

    40/50

    40

    Now lets redo hello.cs as a

    Visual Studio project.

  • 8/10/2019 Dot NET Fundamentals Part2

    41/50

    41

    Visual Studio.NET

    The newest version of Visual Studio

    Multiple language development finally in oneenvironment.

    Can program in

    Visual C#

    Visual Basic.NET

    Visual C++.NET

    Can build

    Desktop console and GUI applications

    Web services

    ASP.NET Web applications

    Mobile applications

  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    46/50

    46

  • 8/10/2019 Dot NET Fundamentals Part2

    47/50

    47

    Homework Part 1

    Send Me An Email

    [email protected], include thefollowing:

    1. Full Name, Nick Name, Student ID #

    2. Home Phone / Work Phone

    3. Email Address (s) [email gives me one]4. Your background in computing/programming

    5. Any experience with .NET, other courses, ?

    6. Your objective or goals for this course

    7. Any material you have a special interest in covering?

    8. Any issues or questions that you have?

    9. Which additional dates you would attend classes on.

    mailto:[email protected]:[email protected]
  • 8/10/2019 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    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 Dot NET Fundamentals Part2

    50/50

    50