This chapter introduces types and variables to meet the following objectives:
Value TypesC# .NET proviides a base set of data types known as Common Type System (CTS). The CTS grouped as
(1) Value Types and (2) Reference Types.
Value types are variables that contain their data stored in an area of memory called the stack.
C# provides three kinds of Value types, they are
1. Primitive Data types (Simple types) or Built-in types
2. Struct types or Uder defined typesand
3. Enumeration Types.
Reference type stores the address on the Stack and the objects (data) on the Heap.
Reference types are Object, Class, Interface, Delegagte, String and Array.
1.Object: The Object type is the most general type in the Framework. You can convert
any type to System.Object. System.Object has four member methods. ToString,
GetType, and Equals members inherited from this type.
2.Class: Class is a template or blue print used to create multiple objects with
similar features.
3.Interface: Interfaces are also known as contracts., The classes must implement the
interface members.
4.Delegate: A delegate is a pointer to a method. Delegate like a class that can hold
a reference to a method.
5.String: Strings are immutable in .Net. Immutable means any change to a string causes
the runtime to create a new string and allocates new object in memory.
S = “Welcome ”;
S += “to ”;
S += “C# “;
S += “World!”;
6.Array: An Array is a collection of objects and they are stored on heap.
Array Declaration
int [] iArray;
iArray = new int[100];
A namespace is a group of related hierarchical classes. The Advantage of using namespaces is, to avoid conflicts when you have multiple classes with the same name. eg:
using System;Conversions can be categorized into Implicit Coonverrsionn, Explicit Conversion, Boxing, and Unboxing.
Implicit Conversion:Boxing and Unboxing is done between Value types and Reference Types
Boxing: Converting value type to an Object type is known as Boxing.
int i =99;
object o = i;
Unboxing : Converting Object type to value type is known as Unboxing.
object o = 98.5;
int i = (int) o;
Expression Types Arithmetic Operator are categorized into Binnary Operator and Unary Operators. Binary Operator Meaning + Addition - Subtraction * Multiplication / Division % Modulus Unary Operator Meaning ++ Incrementing (++i prefix and i++ postifx increemnts) -- Decrementing (--i prefix and i-- postifx decreemnts) Assignment operators assign a value to a variable using the follwinng symbols. Assignment Operators Meaning = Assigning += Addition -= Subtraction *= Multiplication /= Division %= Modulus &= AND |= OR ^= Exclusive OR <<= Left Shift >>= Right Shift A Relational Operator basically takes two values and evaluates the relationship between them. The operator returns a Boolean value. Relational Operators Meaning == Equal to != Not equal < Less than > Greater than <= Less than or equal to >= Greater than or equal to Local Operators are used exclusively with Boolean values. Logical Operators Meaning && AND || OR ! NOT Bitwise Operators Bitwise Operators basically return a Boolean value after the operation has been performed. Bitwise Operators Meaning ! NOT & Bitwise AND | Bitwise OR Ternary Operators Ternary is also known as a conditional operator. Eg: expression1 ? expression2 : expression2