Wednesday, March 20, 2013

Basic C# Types


Basic C# Types

A type is the organization and format of information. C# is a strongly typed language. This essentially means that the compiler and runtime system does a good job of verifying the type consistency of expressions. All variables have a type. The type produced by an expression always either is defined by the C# language or is a user-defined type. C# provides a mechanism for converting one type to another.
C# variables may be declared and initialized in the same statement.
The Simple Types
The simple types consist of Boolean and numeric types. The numeric types are further subdivided into integral and floating-Point types.
The Boolean Type
There's only a single Boolean type named bool. A bool can have a value of either true or false. The values true and false are also the only literal values that you can use for a bool. Here's an example of a bool declaration:
bool isProfitable = true;
The bool type will not accept integer values such as 0, 1, or -1. The keywords true and false are built into the C# language and are the only allowable values.
The Integral Types
The integral types are further subdivided into eight types plus a character type: sbyte, byte, short, ushort, int, uint, long, ulong, and char. All of the integral types except char have signed and unsigned forms. All integral type literals can be expressed in hexadecimal notation by prefixing 0x to a series of hexadecimal numbers 0 through F. The exception is the char.
A char holds a single Unicode character. Some examples of char variable declarations include:
char middleInitial;    \\ uninitialized
char yesNo = 'Y';
char studentGrade = '\u005A';  \\ Unicode 'Z'
char studentGrade = '\x0041';  \\ Unicode 'A'
A byte is an unsigned type that can hold 8 bits of data. Their range is from 0 to 255. An sbyte is a signed byte with a range of –128 to 127. This is how you declare byte variables:
byte age = 25;
sbyte normalizedTolerance = -1;
Table 3 The Integral Types
Type
Size(in Bits)
Range
char
16
0 to 65,535
sbyte
8
–128 to 127
byte
8
0 to 255
short
16
–32,768 to 32,767
ushort
16
0 to 65,535
int
32
–2,147,483,648 to 2,147,483,647
uint
32
0 to 4294967295
long
64
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong
64
0 to 18,446,744,073,709,551,615

Table 4 The Floating-Point Types
Type
Size(bits)
Precision
Range
float
32
7 digits
1.5 x 10–45 to 3.4 x 1038
double
64
15–16 digits
5.0 x 10–324 to 1.7 x 10308
decimal
128
28–29 decimal places
1.0 x 10–28 to 7.9 x 1028

Struct Types
A struct is a value type. All of the types presented thus far fall into the value type category. Value types are variables that directly hold their data. They are allocated on the stack, which makes them very efficient for storing and retrieving information. Structs are containers that can hold a collection of other items. They provide a method for programmers to create their own value types.
Reference Types
There are four reference types in C#: classes, interfaces, delegates, and arrays. Reference types are library or user-defined types that are allocated on the heap. Being allocated on the heap means that reference types use more system resources. They are managed by a built-in garbage collector, which also manages their lifetimes. Classes may contain many other C# language members. They also define unique types.
Interfaces are used to expose the public attributes and behavior of classes. They have no implementations themselves. Whenever a class specifies an interface, a programmer knows, by the definition of that interface, that the class supports certain attributes and behavior. This way, a number of different classes can implement the same interface and be used in the same basic manner but provide their own unique behavior.
Delegates provide a type-safe way to dynamically reference class methods. When the exact method to implement won't be known until runtime, a delegate can be used to accept a reference to a method. Then whatever method is assigned to the delegate at runtime can be executed by calling the delegate to which the method was assigned. This is type-safe because a method must conform to the type specified in the delegate declaration before it is assigned to a delegate.
Arrays provide a method of storing multiple items of a specific type. Their interface represents a linear collection of data that can be referenced in sequence. Their power extends to providing specialized methods for managing their data. A C# array is a useful method of storing many items of the same type of data in a linear form.
Enumeration Types
The enum is a list of constant values. Elements of an enum are expressed in words rather than numbers, which makes it convenient for understanding the meaning of the value being used. Here's an example:
enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };



Implicitly typed local variables

An implicity typed local variable is a feature introduced with C# 3.0. The type of a variable is not explicitly stated but, rather, is defined automatically by a return type. So, instead of:
String  name    = "John Smith";
int     number  = 43;
int[]   numbers = new int[] {1,2,3};
we can use the following:
var name   = "John Smith";
var number = 43;
var numbers = new int[] {1,2,3};
more examples
// i is compiled as an int
var i = 5;
 
// s is compiled as a string
var s = "Hello";
 
// a is compiled as int[]
var a = new[] { 0, 1, 2 };
 
// expr is compiled as IEnumerable<Customer>
// or perhaps IQueryable<Customer>
var expr =
    from c in customers
    where c.City == "London"
    select c;
 
// anon is compiled as an anonymous type
var anon = new { Name = "Terry", Age = 34 };
 
// list is compiled as List<int>                             
var list = new List<int>();
 

0 comments:

Post a Comment