Data Types and Expressions AKEEL AHMED
Overview Identifiers Variable and Literal Types, Classes, and Objects Converting data types Arrays
What is Identifiers? Identifiers are names of elements that appear in a program, such as data items. predefined identifiers System, Main, Console, and WriteLine. user defined identifiers class name like TestClass, method name like sum
C# Keywords/Reserve words From MSDN
Variable and Literal Value A variable represents an area in the computer memory where a value of a particular data type can be stored. Declaring a variable type identifier = expression; int examNumber=23; Literals are the numbers, characters, and combinations of characters used in your program
Types, Classes, and Objects Types: A kind of group that can be differentiated from other kinds of groups by a particular set of characteristics Classes: Types are actually implemented through classes in C#. The Framework class library, which is part of .NET, includes over 2000 classes that you can use in your programs. Objects: an object is an instance of a class
Predefined Data Types
Predefined Data Types
Variables (Syntax) int i = 7; double d=45.8; float fValue= 23f; string message = "Welcome"; object obj = new object(); bool male = false; decimal n3 = 1.234m;
Consts (Syntax) Variable is a place in memory that can be changed const int constValue = 50; constValue = 51; // Will cause compilation error
Converting data types Explicit conversion (also known as cast ) – Programmer forces the conversion and takes the risk of loosing data. byte b = 128; int i = b;
Converting data types Implicit conversion – Conversion is done by the complier automatically. No data is lost because of the conversion. Example: converting byte value (range: 0 to 255) to integer value (range: -2 147 483 648 to 2 147 483 647). decimal pi = 3.14; int notPi = (int)pi;
Converting data types Other conversions – Sometimes one needs to convert not-compatible types – for example convert string value “123” to integer value 123. For such case there are usually special methods. string text = "123"; int number = int.Parse(text); // number == 123
Arrays (Syntax) The array is a complex data type which handles a collection of elements. Each of the elements can be accessed by an index. All the elements of an array must be of the same data type. int[] numbers = new int[5]; numbers[0] = 3; numbers[1] = 2; numbers[2] = 1; numbers[3] = 5; numbers[4] = 6; int len = numbers.Length; for (int i=0; i<len; i++) { Console.WriteLine(numbers[i]); }