Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 2. C# Language Fundamentals Data Types Numeric Types Non-Numeric Types: char and bool Variables Definite Assignment Constants Strings Statements Code.

Similar presentations


Presentation on theme: "1 2. C# Language Fundamentals Data Types Numeric Types Non-Numeric Types: char and bool Variables Definite Assignment Constants Strings Statements Code."— Presentation transcript:

1 1 2. C# Language Fundamentals Data Types Numeric Types Non-Numeric Types: char and bool Variables Definite Assignment Constants Strings Statements Code Samples

2 2 Data Types Common C# intrinsic data types TypeSize in Bytes byte1 bool2 int4 long8 float4 double8 decimal12

3 3 Typing in C# C# is a strongly typed language Types come in two flavours Value (intrinsic) Reference (classes …) Each type has a name (int) and size (4b) The.Net equivalents for int is Int32

4 4 Numeric Data Types Unsigned (positive) byte, ushort, uint, ulong Signed (positive or negative) short, int, long, float, decimal, double Select the smallest type that will hold the required range of numbers

5 5 Non-Numeric Types: char and bool char Holds just a single character Can hold: Simple character (A) Unicode character (u\0041) Escape character (\n) bool Holds true or false in one byte

6 6 Declaring Local Variables int myInt; System.Console.WriteLine("Uninitialized, myInt: {0}",myInt); myInt = 5; int mySecondInt = 10; // declare and initialise int myInt4,myInt5; // declare multiple variables What is the value on an integer before it is initialised?

7 7 Declaring Constants const int FreezingPoint = 32; // degrees Farenheit const int BoilingPoint = 212; Why would you create constants?

8 8 Declaring Enumerations // declare the enumeration enum Temperatures:int { WickedCold = 0, FreezingPoint = 32, LightJacketWeather = 60, SwimmingWeather = 72, BoilingPoint = 212, } An enumeration is a set of named constants Why would you create enumerations? The data type defaults to int

9 9 Using Enumerations System.Console.WriteLine("Freezing point of water: {0}", (int) Temperatures.FreezingPoint ); System.Console.WriteLine("Boiling point of water: {0}", (int) Temperatures.BoilingPoint );

10 10 Declaring Strings string myString = Hello World ;// declare and initialise string Where would you use strings in your code? A string is an object

11 11 Statements, Expressions & White Space A statement ends in a semicolon int myInt = 23; An expression can be part of an assignment myInt = myInt * 23; White spaces are ignored myInt = myInt * 100;

12 12 Unit 2 Lab To write statements that prompt and greet the user 1. Open Visual Studio.Net and create a new C# Console Application project 2. In the Main method insert the following line: string myName; 3. Write a statement that prompts users for their name. 4. Write another statement that reads the users response from the keyboard and assigns it to the myName string. 5. Add one more statement that prints Hello myName to the screen (where myName is the name the user typed in). 6. Save your work.

13 13 Unit 2 Lab … When completed, the Main method should contain the following: static void Main( ) { string myName; Console.WriteLine("Please enter your name"); myName = Console.ReadLine( ); Console.WriteLine("Hello {0}", myName); }


Download ppt "1 2. C# Language Fundamentals Data Types Numeric Types Non-Numeric Types: char and bool Variables Definite Assignment Constants Strings Statements Code."

Similar presentations


Ads by Google