C# Lab01
C# Lab01
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
2
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();
}
}
using System;
using System.Text;
class Example1
{
static void Main(string[] args)
{
3
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;
}
}
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)
4
{
int temp = 5;
refer.valueVal = temp * 2;
}
}
using System;
class MainClass
{
public static void Main()
{
DateTime dt = DateTime.Now; // obtain current time
6
Console.WriteLine("u format: {0:u}", dt);
Console.WriteLine("U format: {0:U}", dt);
}
7
}
}
}