Friday, September 23, 2011

Classes and Structs in C#

Introduction

There are two main concrete ways of encapsulating data and behavior in the common type system of the .NET Framework: classes and structs. Each one offers benefits over the other, and in this article I am going to briefly introduce these two concepts and discuss how to use each and when to use each based on their differences.

Each of these defines a schema for an object that we can create encapsulating the data and behavior that we want into these objects that we can create at runtime. We are able to create these objects and use them in our code freely at runtime. We can sometimes change the values, use them, pass them around. In object oriented programming, classes and structs are an integral piece of the programming paradigm.

Classes

Classes are the most commonly used of these two forms of encapsulation. The main difference between the two is that classes are a reference type, which means that by default they're used by reference instead of by value.

Structs

Structs are the lesser used of these two forms of encapsulation. The primary distinction of structs is that a struct is a value type, which means that it is stored on the stack instead of the heap and it is passed by value instead of by reference by default.

Structs are behave similarly to the other value types that we see when writing .NET code. These include: integers, doubles, bools, and other value type objects.

Structs don't require constructors, but if there is one, they require that there are parameters in the constructor.

Structs do not require the use of the new keyword in creation, but if you use this syntax you need to initialize the members manually.

Structs do not have access to inheritance.

When to Use Each

Classes are generally used for complex data with lots of behavior as well as for objects which may become large. One major reason for this is that classes never need to be copied when passing them around this means that the same object gets reused and we don't have to be concerned with duplicating such a large object.

Structs are generally used in situations where we need light, quick objects which are not often changed and are used in context. They are less often used when there will be a large amount of behavior associated with the objects.

One great benefit of structs is the avoidance of allocating to the heap and thus the overhead of garbage collection is avoided, but be mindful of doing this with large objects as passing them to methods will require copying them since they are value types.

Passing data to and from programs running in c or c++ is one instance where structs are used instead of classes. These are the objects able to do this work for us.


For detail click here

Tuesday, September 20, 2011

Reset Identity column in SQL Server and Mysql

This is one of those simple tip posts that may seem obvious and taken for granted by those of us who have been working with SQL Server for a while now but maybe a newbie or two out there will find this helpful.
Every so often (just this morning!) I find myself resetting an identity column value back to 0after I've deleted all the existing records so the table gets a fresh start at primary key 1. Yes, I know all about primary keys not changing and how the value in the primary key doesn't matter and so on. Sometimes I just like the primary keys starting at 1.
The following line resets the Identity value for the Customer table to 0 so that the next record added starts at 1.
DBCC CHECKIDENT('Customer', RESEED, 0)
This is how we can reset the identity column in SQL Server....
For Mysql, we have a bit different command for resetting identity column
ALTER TABLE tablename AUTO_INCREMENT=0
However, if you have existing rows, the AUTO_INCREMENT value will only reset to one higher than the maximum current ID. For example, if you have a table with the following entries…
ID
First name
3
Peter
6
Paul
7
Mary
… and the AUTO_INCREMENT count is set at 60, the statement to reset the AUTO_INCREMENT count to 0 will only reset it to 8. Only if there are no rows remaining will it reset to 0.
Earn Money ! Affiliate Program
Open Directory Project at dmoz.org