Lab #1 C# Basic Sheet s Owner Student ID Name Signature Group partner 1. Identifier Naming Rules in C# A name must consist of only letters (A Z,a z), digits (0 9), or underscores ( ) The first character must be either a letter or an underscore A name can be at most 63 characters in length A name must not be identical to a reserved word such as class, namespace, int, void, static Exercise 1.1: Consider the following names and check if each of them is a valid name for a C# identifier Identifier Name Valid in C#? Reason XXX yes not violating the naming rules $$$ no consists of special characters $ Y string i j Student ID HelloWorld!! first-time null 204111Class Section3 section3 w*h do 2. Basic Structure of C# Programs Here is an example of C# program structure. namespace (A) { class (B) { static void Main () { (C) Lab #1 1 of 8
From the above, the positions (A), (B), and (C) have the following meanings: (A) is for the namespace s name (B) is for the class s name (C) is for statements telling the computer what to do For simple programs, the namespace part can even be omitted as follows. class (B) { static void Main () { (C) Exercise 2.1: Complete the following tasks: Write a program that can compile and run. Your program must meet these requirements: namespace has the name TestingNamespace class has the name TestingClass The program outputs no results Write another program that can also compile and run, but now it must meet these requirements: There is no namespace class has the name SayHello The program outputs a phrase Hello Section 3 Lab #1 2 of 8
3. Data Types in C# C# provides a number of data types for storing different kinds of data. Some commonly used types are listed in the table below. Type char bool Description Single character Truth value true or false byte Unsigned integer between 0 and 255 int Signed integer between -2,147,483,648 and 2,147,483,647 uint Unsigned integer between 0 and 4,294,967,295 long Signed integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 ulong Unsigned integer between 0 and 18446744073709551615 float Real number double Double-precision real number string Sequence of characters 4. Variables and Constants A variable is an identifier used to represent data that can be changed while the program is running. In C#, a variable must be declared with a specific type as follows: DataType variablename ; where variablename is the name of the variable, and DataType specifies the type of data the variable can store. In addition, a variable can be declared along with its initial value. For this case, the declaration is of the form: DataType variablename = initialvalue ; where initialvalue indicates the initial value of the variable variablename. Example 4.1: Declare a variable distance to be of type uint without an initial value uint myvar ; Example 4.2: Declare a variable salary to be of type long with an initial value 30000 long myvar = 30000; Exercise 4.1: Declare a variable x to be of type float without an initial value Exercise 4.2: Declare a variable myname to be of type string with an initial value "Peter" Lab #1 3 of 8
A constant is an identifier used to represent data that can be used throughout the program. Unlike variables, the value of a constant is fixed and cannot be changed. Constants are useful when we want to use the same value over and over again, but we don t want to write out that value every time it is referred to. In C#, a constant must be declared with a specific type, an initial value, and a keyword const. DataType constantname = value ; where constantname is the name of the constant, DataType indicates the type of data the constant represents, and value specifies the value of the constant. Example 4.3: Declare a constant myconst to be of type double and have a fixed value of 2.71828 const double myconst = 2.71828; Exercise 4.3: Declare a constant PI to be of type double and have a fixed value of 3.1415926535 Example 4.4: More examples of variable and constant declaration. Note that multiple variables (or constants) of the same type can be declared at the same time by separating them with commas. const int FreezingPoint = 32; int x, y; int wd = 5, ht = 8; const double PI = 3.1424; char ch = A ; string mynote = " Hello, Kitty "; int j = 5; Exercise 4.4: Create a new file in SharpDevelop and type the following source code using System ; class CircleArea { static void Main () { const double PI = 3.1415926535; radius = 12. 5; area = PI * radius * radius ; Console. WriteLine (" Circle area = {0 ", area ); Lab #1 4 of 8
Then try to compile the program. What errors are reported, and at which lines? What do you think is the cause? Fix the above code so that it compiles and runs. How did you fix the errors? Exercise 4.5: Create a new file in SharpDevelop and type the following source code using System ; class MoreExercise { static void Main () { int f = 1. 5; char s = " Harry Potter "; char c = A ; Console. WriteLine ("f value = {0, s = {1, c = {2 ", f, s, c); Fix the above code by changing only data types in the variable declarations, so that it compiles and runs. How did you fix the errors? Lab #1 5 of 8
Exercise 4.6: For each of the data item given below, choose an appropriate data type and write a C# statement to declare the specified variable Variable myage to store your own age Variable income to keep track of Peter s personal income Variable temp c to store temperature in degree Celcius Variable temp k to store temperature in Kelvin Variable name to store Aum s full name 5. Arithmetic Expressions An expression is a single term or a combination of terms that can be evaluated to another value. Below are examples of single-term expressions: A number, such as 3000, 1.414 A string literal, such as "Hello, World" A boolean keyword, which can be true and false A single variable or constant, such as myname, salary In this lesson, we will mainly focus on those expressions that evaluate to numerical values. These expressions are called arithmetic expressions. C# provides a collection of arithmetic operators that allow us to build a more complex expressions from simpler ones. Arithmetic operators in C# are as follows: Lab #1 6 of 8
Operator Symbol Type Example Value when x = 20 Add + Binary x+4 24 Subtract - Binary 32-x 12 Multiply * Binary x*2 40 Divide / Binary x/2 10 Modulo % Binary x/6 2 Negate - Unary -x -20 Group () N/A (x+2)*3 66 When more than one arithmatic operators are used within the same expression, C# evaluates the expression in specific order, according to the following precedence rules: 1. ( ) 2. *, / and % 3. + and - 4. For equal precedence, evaluate from left to right Exercise 5.1: Create a new file in SharpDevelop and type the following source code 1 using System ; 2 class Test { 3 static void Main () { 4 double x = 3.0, y = 2. 0; 5 int a = 10, b = 2; 6 double tmp = a+b+x+y; 7 Console. WriteLine ( tmp ); 8 9 Console. ReadLine (); 10 11 Notice that line 6 is blank. Now fill the blank with each of the statements listed in the table below, one at a time. Run the program and put the last result in the right column. Statement Result Console.WriteLine(a); 10 Console.WriteLine(x+a); Console.WriteLine(a/b); Console.WriteLine(y/x); Console.WriteLine(y%x); Console.WriteLine((a+b)/b%a); Console.WriteLine(9.0/5.0*(a-x)); Console.WriteLine(x+y-x*y%x); Console.WriteLine(57%50/25); Lab #1 7 of 8
6. Output Statements Exercise 6.1: (Basic usage of Console.Write and Console.WriteLine) Write a C# program that outputs the following result: Live as if you were to die tomorrow. Learn as if you were to live forever. -Mahatma Gandhi Then copy the code you wrote in SharpDevelopinto the box below. Exercise 6.2: (Advanced usage of Console.Write and Console.WriteLine) Consider the following incomplete code: 1 using System ; 2 3 class SayHi { 4 static void Main () { 5 string yourname = (a) ; 6 uint yourage = (b) ; 7 Console. WriteLine (" Hello {1. You are {0 years old.", 8 (c), (d) ); 9 10 Fill in the blanks (1),..., (4) so that the program will say hello to you and print out your own age. For example, if your name is Arthur and you are 18 years old, the result should be: Hello Arthur. You are 18 years old. Now copy what you filled in the blanks into this table. Position (a) Your answer (b) (c) (d) Lab #1 8 of 8