1 / 13

2. C# Language Fundamentals

2. C# Language Fundamentals. Data Types Numeric Types Non-Numeric Types: char and bool Variables Definite Assignment Constants Strings Statements. Code Samples. Data Types. Common C# intrinsic data types. Typing in C#. C# is a strongly typed language Types come in two flavours

reedjohn
Download Presentation

2. C# Language Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Data Types • Common C# intrinsic data types

  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. 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. 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. Declaring Local Variables int myInt; System.Console.WriteLine("Uninitialized, myInt: {0}",myInt); myInt = 5; int mySecondInt = 10; // declareand initialise int myInt4,myInt5;// declaremultiple variables What is the value on an integer before it is initialised?

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

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

  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. Declaring Strings A string is an object string myString = “Hello World” ;// declareand initialise string Where would you use strings in your code?

  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. 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 user’s 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. 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); }

More Related