Powered by Yoga Guru

Friday, July 16, 2010

C Sharp data type list with description.

C# allows you to define two types of variables: value types and reference types. The value types hold actual values, while reference types hold references to values stored somewhere in memory.

Also value types are allocated on the stack and are available in most programming languages. Reference types are allocated on the heap and typically represent class instances.


Predefined C# value types
sbyte: Holds 8-bit signed integers. The s in sbyte stands for signed, meaning that the variable's value can be either positive or negative. The smallest possible value for ansbyte variable is -128; the largest possible value is 127.
byte: Holds 8-bit unsigned integers. Unlike sbyte variables, byte variables are not signed and can only hold positive numbers. The smallest possible value for a byte variable is 0; the largest possible value is 255.
short: Holds 16-bit signed integers. The smallest possible value for a short variable is -32,768; the largest possible value is 32,767.
ushort: Holds 16-bit unsigned integers. The u in ushort stands for unsigned. The smallest possible value of an ushort variable is 0; the largest possible value is 65,535.
int: Holds 32-bit signed integers. The smallest possible value of an int variable is -2,147,483,648; the largest possible value is 2,147,483,647.
uint: Holds 32-bit unsigned integers. The u in uint stands for unsigned. The smallest possible value of a uint variable is 0; the largest possible value is 4,294,967,295.
long: Holds 64-bit signed integers. The smallest possible value of a long variable is 9,223,372,036,854,775,808; the largest possible value is 9,223,372,036,854,775,807.
ulong: Holds 64-bit unsigned integers. The u in ulong stands for unsigned. The smallest possible value of a ulong variable is 0; the largest possible value is 18,446,744,073,709,551,615.
char: Holds 16-bit Unicode characters. The smallest possible value of a char variable is the Unicode character whose value is 0; the largest possible value is the Unicode character whose value is 65,535.
float: Holds a 32-bit signed floating-point value. The smallest possible value of a float type is approximately 1.5 times 10 to the 45th power; the largest possible value is approximately 3.4 times 10 to the 38th power.
double: Holds a 64-bit signed floating-point value. The smallest possible value of a double is approximately 5 times 10 to the 324th; the largest possible value is approximately 1.7 times 10 to the 308th.
decimal: Holds a 128-bit signed floating-point value. Variables of type decimal are good for financial calculations. The smallest possible value of a decimal type is approximately 1 times 10 to the 28th power; the largest possible value is approximately 7.9 times 10 to the 28th power.
bool: Holds one of two possible values, true or false. The use of the bool type is one of the areas in which C# breaks from its C and C++ heritage. In C and C++, the integer value 0 was synonymous with false, and any nonzero value was synonymous with true. In C#, however, the types are not synonymous. You cannot convert an integer variable into an equivalent bool value. If you want to work with a variable that needs to represent a true or false condition, use a bool variable and not an int variable.


Predefined C# reference types

string: Represents a string of Unicode characters. It allows easy manipulation and assignment of strings. Strings are immutable, meaning that once it is created it can't be modified. So when you try to modify a string, such as concatenating it with another string, a new string object is actually created to hold the new resulting string.
object: Represents a general purpose type. In C#, all predefined and user-defined types inherit from the object type or System.Object class.


Compound data types
The concept of a class as a compound data type with fields, methods, and events is similar in Java and C#. (Class inheritance is discussed separately in the topic entitled Inheritance and Derived Classes (C# vs Java).) C# introduces the concept of a struct as a stack-allocated compound data type that does not support inheritance. In most other respects, structs are very similar to classes. Structs provide a lightweight way of grouping together related fields and methods for use in tight loops and other scenarios where performance is critical.
C# enables you to create a destructor method that is called before instances of a class are garbage-collected. In Java, afinalize method can be used to contain code that cleans up resources before the object is garbage-collected. In C#, this function is performed by the class destructor. The destructor resembles a constructor with no arguments and a preceding tilde character (~).
Built-In Data Types
C# provides all the data types that are available in Java, and adds support for unsigned numerals and a new 128-bit high-precision floating-point type.
For each primitive data type in Java, the core class library provides a wrapper class that represents it as a Java object. For example, the Int32 class wraps the int data type, and the Double class wraps the double data type.
On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int is the short name for System.Int32 and double is the short form ofSystem.Double.


Constants
Both Java and C# provide the ability to declare a variable whose value is specified at compile time and cannot be changed at runtime. Java uses the final field modifier to declare such a variable, while C# uses the const keyword. In addition to const, C# provides the readonly keyword to declare variables that can be assigned a value once at runtime--either in the declaration statement or else in the constructor. After initialization, the value of a readonly variable cannot change. One scenario in which readonly variables are useful is when modules that have been compiled separately need to share data such as a version number. If module A is updated and recompiled with a new version number, module B can be initialized with that new constant value without having to be recompiled.



Strings
String types in both Java and C# exhibit similar behavior with slight differences. Both string types are immutable, meaning that the values of the strings cannot be changed once the strings have been created. In both instances, methods that appear to modify the actual content of a string actually create a new string to return, leaving the original string unchanged. The process of comparing string values is different in C# and Java. To compare string values in Java, developers need to call the equals method on a string type as the == operator compares reference types by default. In C#, developers can use the == or != operators to compare string values directly. Even though a string is a reference type in C#, the == and != operator will, by default, compare the string values rather then references.
Just like in Java, C# developers should not use the string type for concatenating strings to avoid the overhead of creating new string classes every time the string is concatenated. Instead, developers can use the StringBuilder class, which is functionally equivalent to the Java StringBuffer class.


enum


An enum is a value type with a set of related named constants often referred to as an enumerator list. They allow code to look a lot cleaner and easier to read by getting rid of "magic numbers", that is to say, they get rid of numbers which have a purpose within a module of code, but make the code harder to read. If a single number needs a definition to make it easier to read then use a constant as shown below.


An enum is declared as follows

No comments:

Post a Comment