Chapter 2Getting Started with C#
What is a variable?
Variable: imagine like a box to store one thing (data)
Eg: int age; age = 5;
What is a variable?
Variable: imagine like a box to store one thing (data)
Eg: int age; age = 5;
int - Size of variable must be big enough to store Integer
What is a variable?
Variable: imagine like a box to store one thing (data)
Eg: int age; age = 5;
int - Size of variable must be big enough to store Integerage - Name of variable
age
What is a variable?
Variable: imagine like a box to store one thing (data)
Eg: int age; age = 5;
int - Size of variable must be big enough to store Integerage - Name of variable
age = 5 – Put a data 5 into the variable
age
5
Value Type Vs Reference Type
Variables: two types◦Value type (simple type like what you just saw)
Only need to store one thing (5, 3.5, true/false, ‘C’ and “string”)
◦Reference type (complex type for objects) Need to store more than one thing (age + height
+ run() + … )
Reference Type
Reference type (complex type for objects)Eg: Human john; john = new Human();
Compare this to int age;age = 5;
Reference Type
Reference type (complex type for objects)Eg: Human john; john = new Human();
Human - Size of variable must be big enough to store an Address
john
Reference Type
Reference type (complex type for objects)Eg: Human john; john = new Human();
Human - Size of variable must be big enough to store an Addressjohn - Name of variable
Reference Type
Reference type (complex type for objects)Eg: Human john; john = new Human();
Human - Size of variable must be big enough to store an Addressjohn - Name of variable
john = new Human() – Get a house with enough space for john (age, height, etc)
john
D403
age
height
…
Non-Static Vs Static Class
Non-Static: need New() to instantiate / create an object – like what you see just now
Static: NO need to use New() to use, there is just one copy of the class. This type of class basically to provide special functions for other objects
So if you see a class being used without New(): it is a static class
Eg Math class age = Math.Round(18.5); // Math rounding
First Console Program
A new project:
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
What is a namespace?It is simply a logical collection of related classes. Egnamespace System{ public static class Console { …. // with properties and methods } class xxxx { …. // with properties and methods }}
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
What is a namespace?It is simply a logical collection of related classes. Egnamespace System{ public static class Console { …. // with properties and methods } class xxxx { …. // with properties and methods }}
Access Modifiers – on class, its properties and methodsTypes: public, private, protected
public: Access is not restricted.private: Access is limited to the containing type.protected: Access is limited to the containing class or types derived from the containing class.
Group Exercise
public class Apple{ public OutputVariety() { … } protected ReadColour() { …. } private ResetColour() { …. }}class AnotherClass{ …. }class DerivedClass : Apple { …. }
Which class could access the 3 methods?
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
Inside a namespace, may contain internal namespace.namespace System{ namespace Data { …. // with classes & their properties and methods }}
OR combine with a “.”Namespace System.Data{ …. // with classes & their properties and methods}
Group Exercise
namespace ITE.MP.GDD{ public class 2W { public int ClassSize; }}namespace ITE.MP.EC{ public class 2W { public int ClassSize; }}
How to access ClassSize for one ITE.MP.GDD.2W object?
Add code and run program
Add following code into main()
Console.WriteLine("Welcome!");
> View >output> Build > Build Solution> Debug > Start Without Debugging
Understanding Program.cs
Demo: Remove all “using” statements and correct error - instead of Console.WriteLine(), change to System.Console.WriteLine()
Demo: Using refactor to rename “Program” to “HelloWorld”
Demo: Right click on Console > Go To Definition- take a look at the Console class and its WriteLine method
Main(string[] args)
string[] args // string array // with name args
Getting inputs from commandline
Console.WriteLine("Hello " + args[0]);
•First argument
args[0]
•Second argument
args[1]•T
hird argument
args[2]
Setting Arguments
Argument is what user key in after the program file
Eg for Unreal Tournament, to use the editor, user type “ut.exe editor” => editor is an argument
In console program, there are two ways to key in arguments for running the program:
1. Using IDE 2. Use command prompt
Setting arguments using IDE
In Solution Explorer, right click the project and select Properties:
Setting arguments using cmd
Start > run > cmdUse cd, dir to move to the projet debug
folder: …. myFirstProgram\bin\Debug>> myFirstProgram.exe joe
C# Application Programming Interface (API)
C# APIor.NET Framework Class Library Reference
http://msdn.microsoft.com/en-us/library/ms229335.aspx
Recap
Console program:◦Simple◦Procedural (from top to bottom)◦Inputs:
Arguments: How? Eg Unreal Tournament Editor: > UT.exe editor
More useful to take inputs from Keyboard: How?
Guided Hands on
Launch Visual Studio 2008 – C#Create a new console projectAdd the following line into main(..)
Console.WriteLine("Hello " + args[0]);Add argument “James“Build and run
Get input from keyboard
static void Main() { string str; // A string variable to hold input Console.Write(“Your input:");
str = Console.ReadLine(); // Wait for inputs
Console.WriteLine("You entered: " + str); }
Exercise 2.1
Create your first console programRemove the unused namespacesUse refactor to rename your class to YourFullName Refer to the following URL for Naming Convention
for Class: http://alturl.com/6uzp
http://alturl.com/o7fi
What you see on screen (blue by computer, red by user)
Your Input: 3Output: 3 3 3 3 3
Type conversion
Need to tell computer to convert fromstring to integer/double
string str; // a string variablestr = Console.Readline(); // get inputint numberInteger; // an integer variable// convert string type to integer typenumberInteger = int.Parse(str);double numberDouble; // a decimal variable// convert string type to decimal typenumberDouble = double.Parse(str)
Exercise 2.2
Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This program can use only 2 variables.
Hint 1: Use decimal instead of integer eg: double myNumber; Hint 2: conversion from string to double eg: myNumber = double.Parse(str);
=> Next page for screen output
Exercise 2.2
What you see on screen (blue by computer, red by user)Input1: 1Input2: 4Input3: 4Input4: 2Input5: 3Average: 2.8
Exercise 2.3
Write a program that asks the user to type the width and the length of a rectangle and then outputs to the screen the area and the perimeter of that rectangle.Hint: 1) Assume that the width and length
are integers 2) eg: width = int.Parse(str);
3) Operator for multiplication: * eg: area = width * length;=> Next page for screen output
Exercise 2.3
What you see on screen (blue by computer, red by user)Width: 5Length: 4Area:20 and Perimeter:18
Exercise 2.4
Write a program that asks the user to type 2 integers A and B and exchange the value of A and B.
Hint:1) To swop 2 variable, you need another variable to store one of the value 2) Eg for ascending order (small to Big) if (n1 > n2) { n3 = n2; n2 = n1; n1 = n3; }
“then” processing
Exercise 2.4
What you see on screen (blue by computer, red by user)Input1: 1Input2: 4Input1: 4 Input2: 1
Exercise 2.5
Prompt user to key in 3 integers.Sort the integers in ascending order.Print the 3 integers as you sort.Eg:Input1: 2Input2: 1Input3: 0210120102012
if (n1 > n2) { … }
if (n2 > n3) { … }
if (n1 > n2) { … }
Summary
C# Language Fundamentals covered:
int, double, string, Console.WriteLine(), Console.Write(), Console.ReadLine(), int.Parse(), double.Parse(), simple if then statement
Problem solving skills covered:
repeating steps swopping of 2
variables simple sorting