Week2a BasicElementsinC#
Week2a BasicElementsinC#
– class
– Main
– types
– variables
– operators
Class
– specify name
– enclose body in { }
Main
Basic Output
The first line of the program using System; - the using keyword is used to include the System
namespace in the program. A program generally has multiple using statements.
The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorld
namespace contains the class HelloWorld.
The next line has a class declaration, the class HelloWorld contains the data and method definitions
that your program uses. Classes generally contain multiple methods. Methods define the behavior of the
class. However, the HelloWorld class has only one method Main.
The next line defines the Main method, which is the entry point for all C# programs. The Main
method states what the class does when executed.
The next line /*...*/ is ignored by the compiler and it is put to add comments in the program. The
Main method specifies its behavior with the statement Console.WriteLine("Hello World"); WriteLine is
a method of the Console class defined in the System namespace. This statement causes the message
"Hello, World!" to be displayed on the screen.
The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press
and it prevents the screen from running and closing quickly when the program is launched from Visual
Studio .NET.
NOTES:
C# is case sensitive.
Unlike Java, program file name could be different from the class name.
Variables Declarations
Data types – specify the type and range of data assign to a variable
- Integer types – sbyte, byte, short, ushort, int, uint, long, ulong;
- String – string;
These data types are called primitive (built-in types), because they are embedded in C# language at the
lowest level. The table below represents the above mentioned data types, their range and their default
values:
Example variable declarations:
Syntax:
area = 3.2;
x = 2;
y = 0;
z = 10;
type = ‘C’;
4. The underscore symbol (_) is considered as a letter in C. It is not recommended to be used, however,
as the first character in a name.
Basic Input
NOTE: all inputs are string, conversion of data is needed using the convert class
-lines encircle in red are variable declarations of input_value and name for string data type and
-lines encircle in blue ask the user to input a number assign to input_value variable,
input_value = Console.ReadLine();
since all inputs are string we need to convert it into an integer before we assign it to num
conversion:
num = Convert.ToInt32(input_value);
-lines encircle in yellow ask the user to input a string assign to name variable, no conversion
needed since our data type is a string.
Some conversion using Convert class:
Type conversion
Implicit Conversion – from larger type (base on range of the data type) to smaller type (base on range of
the data type)
Example:
To return a remainder
z = x % y;
z has a value of 2 since 5/3 is 1 remainder 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ComputeTravel
{
class Program
{
static void Main(string[] args)
{
//Computes and outputs travel time
//for a given speed and distance
// variable declaration
int speed; //rate of travel
double distance; //miles to travel
double time; //time needed for this travel
string input_value;
// ask input for speed
Console.Write("Enter your speed in mph: ");
input_value = Console.ReadLine();
speed = Convert.ToInt32(input_value);
Console.ReadKey();
}
}
}
Practice Exercises
1. Write a program that would accept the number of bottles and cans then compute
the amount of soda (Liters) in the refrigerator. The bottle has two liter while
the can is 12oz.
1 (fluid) oz = 29.586mL
1 gallon = 3.785L
2. Write a program that will input a 5 digit integer and print the place value for each digit.
Sample output:
Enter a 5 digit no.: 12345
12 Thousands
3 Hundreds
4 Tens
5 Ones
Hint: Use the Modulo operator % to get the remainder and / to divide
Exercises (Graded)
1. Write a program that accept an integer representing the total number of seconds and
output the equivalent time in hours, minutes, and seconds.
3. Write a program that extracts and prints the second rightmost digit of the integral portion of a float.
Example:
input: 723.56
output: 2