Posts

Showing posts from 2014

What is C#?

C# is an object oriented, type safe and managed language that is compiled by .Net framework to generate Microsoft Intermediate Language.

Difference between Destructor, Dispose and Finalize

Destructor They are special method that contains clean up code for the object.We can't call them explicitly in our code as they are implicitly called by GC(Garbage Collector).In c# they have same name as the class name preceded by the "~" sign. class MyClass     {         public MyClass()         { }         ~MyClass()         { }     } Dispose These are like any other methods in the class and can be called explicitly but they have a special purpose of cleaning up the object.In the dispose method we write clean up code for the object.It is important that we freed up all the unmanaged resources in the dispose method like database connections,files, etc.. The class implementing dispose method should implement IDisposable which is inherited by interface and it contains GC.SupressFinalize() method for the object it is disposing if the class has destructor because it has already done the work to clean up the object ,then it is not necessary for the garbage colle

When to use Truncate and Delete Command in SQL

INTRODUCTION Truncate and Delete in SQL are two commands which is used to remove or delete data from table. Though quite basic in nature both Sql commands can create lot of trouble until you are familiar with details before using it. An Incorrect choice of command can result is either very slow process or can even blew up log segment, if too much data needs to be removed and log segment is not enough. That's why it's critical to know when to use truncate and delete command in SQL but before using these you should be aware of the Differences between Truncate and Delete , and based upon them, we should be able to find out when DELETE is better option for removing data or TRUNCATE should be used to purge tables. I have still seen people firing delete command just to empty a table with millions of records which eventually lock the whole table for doing anything and take ages to complete or Simply blew log segment or hang the machine. Truncate Use truncate table if you need

Difference Between Delete & Truncate in Sql Server

INTRODUCTION While working on database, we are using Delete and Truncate without knowing the differences between them and when to use them . In this article we will discuss the difference between Delete and Truncate in Sql. Delete Delete is a DML command. Delete statement is executed using a row lock,each row in the table is locked for deletion. We can specify filters in where clause. It deletes specified data if where condition exists. Delete activities a trigger because the operation are logged individually. Slower than Truncate because it Keeps logs Truncate Truncate is a DDL command. Truncate table always lock the table and page but not each row.As it removes all the data. Cannot use Where condition.  It Removes all the data. Truncate table cannot activate a trigger because the operation does not log individual row deletions. Faster in performance wise, because it doesn't keep any logs. Note Delete and Truncate both can be rolled back when used with Tr

Rollback Table after Truncate Command in Sql Server

Image
It is misconception among people that Truncate cannot be rolled back. But in reality Truncate operation can be Rolled Backed before Commit.Here is the Sql query -- Create table  CREATE TABLE Employee                   ( EmpID INT PRIMARY KEY ,                     EmpSalary INT                   ) -- Check data before Truncate SELECT * FROM Employee --Begin  Transaction BEGIN TRAN --Truncate Table TRUNCATE TABLE Employee GO --Check data after Truncate SELECT * FROM Employee GO --Rollback Transaction ROLLBACK TRAN GO --Check the data after Rollback SELECT * FROM Employee GO Note:- If Transaction is done, mean Committed then we can not rollback Truncate command from log file but we can restore data using MSSQL Server Management Studio. Check for difference between Delete and Truncate in next article. Give your valuable comments it's encourage me to publish new articles.

Method Parameter in C#

C# is having 4 parameter types which are: Value Parameter : Default parameter types.Only Input Reference (ref) Parameter : Input / Output Output (out) Parameter  Parameter (params) Arrays Value Parameter      Are used for passing parameters into methods by value.When a method is invoked, the values of actual parameter are assigned to the corresponding formal parameter. The values can be changed with in the method. The value of the actual parameter that is passed by value to a method is not changed made to the corresponding formal parameter within of the method. This is because the methods refer to only copies of those variables when they are passed by value. Reference Parameter (Pass By Reference)      Used to pass parameters, a reference parameter does not create a new storage location. Instead it represents the same storage location as the actual parameter used in the method invocation. Reference parameter is used in situations where we would like to change the value

Boxing & Unboxing in C#

Boxing is an implicit conversion of a value type to the type object.                       int i = 123;  // A value type                       object box = i; // Boxing Unboxing is an explicit conversion from the type object to a variable type.                       int i = 123; // A value type                       object box = i; // Boxing                       int j = (int)box; // Unboxing Casting: casting is the process of converting a variable from one type to another( from a string to an integer)  In a simple language we can define: Boxing -  converting   value type to reference type  Unboxing - converting reference type to value type. Hope this will make you understand Boxing and Unboxing in C#.

Modifiers in C#

Here are the modifiers with description and where they are applies: Modifier Applies To Description New Function Members The member hides an inherited member with the same signature. Static All members The member does not operate on a specific instance of the class. Virtual Classes and Function members only The members can be overridden by a derived class. Abstract Function members only A virtual member that defines the signature of the members, but doesn’t provide an implementation. Override Function members only Members override inherited virtual or abstract members. Sealed Classes Members override an inherited virtual member, but cannot be overridden by any classes that inherit from this class. Must be used in conjunction with override. Extern Static[dll Import] method

Extension Class in C#

Extension method is a new feature in C# 3.0.  Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code. This means that if you want to add some methods into the existing String class you can do it quite easily. Here's a couple of rules to consider when deciding on whether or not to use extension methods:   Extension methods cannot be used to override existing methods. An extension method with the same name and signature as an instance method will not be called.     The concept of extension methods cannot be applied to fields, properties or events. Use extension methods sparingly....overuse can be a bad thing!

Anonymous type in C#

This is a new feature in C# 3.0. This enable use to create a type/class on-the-fly at compile time.  For example: var emp = new { Name = "Sheo", Gender = "Male", Active = true }; In this case a new type will be created on the fly that will have Name, Gender, and Active as public property and its backing field like _Name, _Gender, _Active. This is especially useful when we want to receive data from other object or iterate through a collection and set values and do not want to create a class just to hold the data.  Note that: anonymous types are just a placeholder, we can't customize its behavior or add methods inside it

Use of 'var' keywords in C#

This is the new feature in c# 3.0. This enables us to declare a variable whose type is implicitly inferred from the expression used to initialize the variable. For eg.  var age=10; Because the initialization value(10)  of the variable age is integer type of age will be treated as integer type of variable.  'var' keyword is used for two main reason's: 1. To limit redundant code: Dictionary< string , List< int , bool >> myDictionary = new Dictionary< string ,   List< int , bool >>(); By using 'var' keyword can be written as: var myDictionary = new Dictionary< string , List< int , bool >>(); 2. To support anonymous types var car = new { Name = "ABC", Ad

Use of "using" keyword in C#

Using statement is used to work with an object in C# that inherits IDisposable interface. IDisposable interface has one public method called Dispose that is used to dispose off the object. When we use Using statement, we don't need to explicitly dispose the object in the code, the using statement takes care of it. For eg.           using (SqlConnection conn = new SqlConnection())           {           }           //When we use above block, internally the code is generated like this           SqlConnection conn = new SqlConnection()           try           {           }           finally           {           // calls the dispose method of the conn object           } Using statement make the code more readable and compact.

Difference between Shadow and Override

Most of the time these question(difference's) are asked in interview's. That's why i thought to discuss this with you people. Shadowing: - This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword "Shadows". The method signature , access level and return type of the shadowed member can be completely different than the base class member. Hiding: - This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword "new". The method signature, access level and return type of the hidden member has to be same as the base class member. Comparing the three:- The access level, signature and the return type can only be changed when you are shadowing with VB.NET. Hiding a

Difference between const and readonly

The read only keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A read only field can be initialized either at the declaration or in a constructor. Therefore read only field can have different values depending on the constructor used. Also, while a const field is a compile time constant but the read only field can be used for run time constants.