Thursday, March 11, 2010

struct and a class

Here are just some of the differences between a struct and a class:

* Classes are reference types and structs are value types. Since classes are reference type, a class variable can be assigned null, but we cannot assign null to a struct variable, since structs are value type.
* You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct, you will be dealing directly with struct
* Classes must be instantiated using the new operator, but structs can be instantiated without using the new operator.
* Classes support inheritance.But there is no inheritance for structs, (structs don't support inheritance polymorphism )
* It is not mandatory to initialize all Fields inside the constructor of a class. But all the Fields of a struct must be fully initialized inside the constructor.
* structs object is allocated in the stack, and the class object is allocated in the heap.

Class are usually for large amounts of data, whereas structs are smaller, and often used when you want to group similar data together. Use a class when object identity is more important than value. Use a struct when the value contained by an instance is more important than instance identity.

Struct variables directly contain their values, so when you pass a struct instance as a parameter, it can be more expensive than passing an instance of a reference type, due to
the copying costs.

Friday, March 5, 2010

Char Vs Varchar

The char is a fixed-length[Storage Size] character data type, the varchar is a variable-length character data type.

Because char is a fixed-length data type, the storage size of the char value is equal to the maximum size for this column. Because varchar is a variable-length data type, the storage size of the varchar value is the actual length of the data entered, not the maximum size for this column.

You can use char when the data entries in a column are expected to be the same size.
You can use varchar when the data entries in a column are expected to vary considerably in size.

1. Storage wise: char columns have fixed length. If the user supplied value for the column is less than the fixed length defined in the schema, the column is padded with 0 at end to make the total length fixed. varchar doesn't have a fixed length thus no padding is needed. But as the result varchar columns have to store the size of the data together with the column
data, which takes an extra 2 bytes per varchar column.

2. Performance wise locating char is a little faster than varchar. Since char columns have fixed length, they are stored in fixed location in a row. This means locating a char column can directly jump to the fixed location in a row to read. For varchar column since the size of the data is variable, they can't be stored in fixed location in a row and rather there is some kind of lookup table in the row format to store the location of each varchar column. This means locating a varchar column has to lookup the location of the column in the lookup table stored in the row first before jumping to the location to read. Referencing the lokup table introduces some perofrmance overhead, especially ifthe lookup table reference causes cache line miss.