Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types and Expressions

Similar presentations


Presentation on theme: "Data Types and Expressions"— Presentation transcript:

1 Data Types and Expressions
AKEEL AHMED

2 Overview Identifiers Variable and Literal Types, Classes, and Objects
Converting data types Arrays

3 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

4 C# Keywords/Reserve words
From MSDN

5 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

6 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

7 Predefined Data Types

8 Predefined Data Types

9 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;

10 Consts (Syntax) Variable is a place in memory that can be changed const int constValue = 50; constValue = 51; // Will cause compilation error

11 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;

12 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: to ). decimal pi = 3.14; int notPi = (int)pi;

13 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

14 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]); }


Download ppt "Data Types and Expressions"

Similar presentations


Ads by Google