Bài đăng nổi bật

 




PROGRAMMING IN C#

Module 1: Introduction to C# and VS

Module 2: Variables and Data Types

 

Lab Guide for Lab1



Session Objectives

In this session, you will be practicing with 

  • Data type in C#
  • Input and output data in C#
  • Number and datetime format specifier
  • Implicitly typed local variables
  • Anonymous Types

Part 1 – Getting started (30 minutes)


Exercise 1

Step 1: Open Visual Studio 2008

Step 2: Select the menu File->New->Project to create console based project named ‘First_prg’ and Solution named Session01 as shown in Figure 1

pastedGraphic.png

Figure 1: New Project Dialog Box

Step 3: Rename the class file ‘program.cs‘ to ‘First_prg.cs’

Step 4: Replace code in ‘First_prg.cs’ with given code

using System;

class Example

{

        static void Main(string[] args)

        {

            Console.WriteLine("This is the my first program using C#”);

            Console.ReadLine();

        }

}


Step 5: Select menu File -> Save to save the file

Step 6: Select Build -> Build First_prg  option to build the project

Step 7: Select Debug -> Start without Debuging to execute the program

The output of the program as following

pastedGraphic_1.png

Exercise 2: Using datatype


Step 1: Add a console based project ‘DataType’ to the solution


pastedGraphic_2.png                   pastedGraphic_3.png



Step 2: Right click on project DataTypes -> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘DataTypes.cs’

Step 4: Replace the code in ‘DataTypes.cs’ with the given code


using System;

class Example

{

    static void Main(string[] args)

    {

        int intVal;

        double dblVal;

        string strVal;

        intVal = 10;

        dblVal = 3.142;

        strVal = "Fpt Aptech";

        Console.WriteLine("{0} is an integer value", intVal);

        Console.WriteLine("{0} is an double value", dblVal);

        Console.WriteLine("{0} is an string", strVal);

        Console.ReadLine();

    }

}


Step 5: Select menu File -> Save to save the file

Step 6: Select Build -> Build ‘DataTypes’ option to build the project

Step 7: Select Debug -> Start without Debuging to execute the program

The output of program as following


pastedGraphic_4.png

Exercise 3: value type


Step 1: Add a console based project ‘ValueType’ to the solution

Step 2: Right click on project ValueType-> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘ValueType.cs’

Step 4: Replace the code in ‘ValueType.cs’ with the given code


using System;

using System.Text;


    class Example1

    {

        static void Main(string[] args)

        {

            int valueVal = 5;

            Test(valueVal);

            Console.WriteLine("The value of the variable is {0}",   valueVal);

            Console.ReadLine();

        }

        static void Test(int valueVal)

        {

            int temp = 5;

            valueVal = temp * 2;

        }

    }


Step 5: Select menu File -> Save to save the file

Step 6: Select Build -> Build ‘ValueType’ option to build the project

Step 7: Select Debug -> Start without Debuging to execute the program

The output of program as following


pastedGraphic_5.png


Exercise 3:  Reference Type


Step 1: Add a console based project ‘ReferenceType’ to the solution

Step 2: Right click on project ReferenceType -> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘ReferenceType.cs’

Step 4: Replace the code in ‘ReferenceType.cs’ with the given code


using System;

    class ReferenceType

    {

        public int valueVal;

    }

    class TestReference

    {

        static void Main(string[] args)

        {

            ReferenceType refer = new ReferenceType();

            refer.valueVal = 5;

            Test(refer);

            Console.WriteLine("The value of the variable is {0}", refer.valueVal);

            Console.ReadLine();

        }

        static void Test(ReferenceType refer)

        {

            int temp = 5;

            refer.valueVal = temp * 2;

        }

    }


Step 5: Select menu File -> Save to save the file

Step 6: Select Build -> Build ReferenceType option to build the project

Step 7: Select Debug -> Start without Debuging to execute the program

The output of program as following



pastedGraphic_6.png



Exercise 4:  Number  format specifier


Step 1: Add a console based project ‘NumberFormat’ to the solution

Step 2: Right click on project NumberFormat -> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘NumberFormat.cs’

Step 4: Replace the code in ‘NumberFormat.cs’ with the given code


/*This program demonstrates the the numeric formatting in C#*/

using System;

class NumberFormat

{

    static void Main(string[] args)

    {

        Console.WriteLine("Currency formatting - {0:C} {1:C4}", 88.8, 888.8);

        Console.WriteLine("Integer formatting - {0:D5}", 88);

        Console.WriteLine("Exponential formatting - {0:E}", 888.8);

        Console.WriteLine("Fixed-point formatting - {0:F3}", 888.8888);

        Console.WriteLine("General formatting - {0:G}", 888.8888);

        Console.WriteLine("Number formatting - {0:N}", 8888888.8);

        Console.WriteLine("Hexadecimal formatting - {0:X4}", 88);

    }

}


Step 5: Select menu File -> Save to save the file

Step 6: Select Build -> Build ‘NumberFormat’ option to build the project

Step 7: Select Debug -> Start without Debuging to execute the program

The output of program as following


pastedGraphic_7.png


Exercise 5:  Datetime  format specifier


Step 1: Add a console based project ‘DateTimeFormat’ to the solution

Step 2: Right click on project DateTimeFormat -> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘DateTimeFormat.cs’

Step 4: Replace the code in ‘DateTimeFormat.cs’ with the given code


using System;

class MainClass

{

    public static void Main()

    {

        DateTime dt = DateTime.Now; // obtain current time 


        Console.WriteLine("d format: {0:d}", dt);

        Console.WriteLine("D format: {0:D}", dt);


        Console.WriteLine("t format: {0:t}", dt);

        Console.WriteLine("T format: {0:T}", dt);


        Console.WriteLine("f format: {0:f}", dt);

        Console.WriteLine("F format: {0:F}", dt);


        Console.WriteLine("g format: {0:g}", dt);

        Console.WriteLine("G format: {0:G}", dt);


        Console.WriteLine("m format: {0:m}", dt);

        Console.WriteLine("M format: {0:M}", dt);


        Console.WriteLine("r format: {0:r}", dt);

        Console.WriteLine("R format: {0:R}", dt);


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


        Console.WriteLine("u format: {0:u}", dt);

        Console.WriteLine("U format: {0:U}", dt);


        Console.WriteLine("y format: {0:y}", dt);

        Console.WriteLine("Y format: {0:Y}", dt);

    }

}


Step 5: Select menu File -> Save to save the file

Step 6: Select Build -> Build ‘DateTimeFormat’ option to build the project

Step 7: Select Debug -> Start without Debuging to execute the program

The output of program as following


pastedGraphic_8.png


Exercise 6: Implicitly typed local variables


Step 1: Add a console based project ‘ImplicitilyTypedLocal’ to the solution

Step 2: Right click on project ImplicitilyTypedLocal’ -> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘ImplicitilyTypedLocal.cs’

Step 4: Replace the code in ‘ImplicitilyTypedLocal.cs’ with the given code

class ImplicitilyTypedLocal

    {

        static void Main(string[] args)

        {

            var i = 5;

            var s = "hello";

            var d = 1.0;

            //i is an integer

            Console.WriteLine("i*i: " +i * i);

            //s is a string

            Console.WriteLine("s in upper case:" + s.ToUpper());

            //d is a double

            Console.WriteLine("type of d:" +d.GetType());

            Console.ReadLine();


        }

    }


Step 5: Run the program

Exercise 7: Anonymous Types variables

Step 1: Add a console based project ‘AnonTypes” to the solution

Step 2: Right click on project AnonTypes-> set as Startup project

Step 3: Rename the class file ‘Program.cs’ to ‘AnonTypes.cs’

Step 4: Replace the code in ‘AnonTypes.cs’ with the given code

class AnonTypes

    {

        static void Main(string[] args)

        {

            var p1 = new { Name = "A", Price = 3 };

            Console.WriteLine("Name = {0}\nPrice = {1}",

                              p1.Name.ToLower(), p1.Price);

            Console.ReadLine();


        }

    }


Step 5: Run the program


Post a Comment

أحدث أقدم