Category Archives: C#


Splitter in C#……………….

It is placed after/before control. At run time if u keep mouse control at edge of control a small arrow appears using it you can increase/decrease size of the control. (Control which has expandability).

Procedure :

1)    Increase the form size to maximum, take treeview control, set its dock property to left.

2)    Take splitter

3)    Take listview control, increase the size according to form.


int a, b, c, d;

private void Form6_Load(object sender, EventArgs e)


a = treeView1.Width;

b = listView1.Left;


private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)


c = treeView1.Width;

d = c – a;

listView1.Left = b + d;

listView1.Width = this.Width – listView1.Left;




How to create a SQL Connection in C Sharp……….

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Database
class Program
static void Main(string[] args)
// String copy in notepad from property windows
string conString = @”Data Source=.\SQLEXPRESS;AttachDbFilename=””D:\visual studio2010\Database\Database\emp.mdf””;Integrated Security=True;User Instance=True” ;

SqlConnection con = new SqlConnection(conString);

string sql = “select Fname,Lname,Email,Mobile from employee where IsActive = 1”;

//Fire Querry
SqlCommand cmd = new SqlCommand(sql,con);

//Get Result of Querry
SqlDataReader rd = cmd.ExecuteReader();

//Iterate over all result
string msg = string.Format(“Fname = {0} Lname = {1} Email = {2} \n Mobile = {3}”,rd[“Fname”],rd[“Lname”],rd[“Email”],rd[“Mobile”]);






Difference between Dispose and Finallize method……

The .NET garbage collector manages the memory of managed objects (native .NET objects) but it does not manage, nor is it directly able to clean up unmanaged resources. Managed resources are those that are cleaned up implicitly by the garbage collector. You do not have to write code to release such resources explicitly. In contrast, you must clean up unmanaged resources (file handles, database collections, etc.) explicitly in your code.

There are situations when you might need to allocate memory for unmanaged resources from managed code. As an example, suppose you have to open a database connection from within a class. The database connection instance is an unmanaged resource encapsulated within this class and should be released as soon as you are done with it. In such cases, you’ll need to free the memory occupied by the unmanaged resources explicitly, because the GC doesn’t free them implicitly.

Briefly, the GC works as shown below:

It searches for managed objects that are referenced in managed code.
It then attempts to finalize those objects that are not referenced in the code.
Lastly, it frees the unreferenced objects and reclaims the memory occupied by them.
The GC maintains lists of managed objects arranged in “generations.” A generation is a measure of the relative lifetime of the objects in memory. The generation number indicates to which generation an object belongs. Recently created objects are stored in lower generations compared to those created earlier in the application’s life cycle. Longer-lived objects get promoted to higher generations. Because applications tend to create many short-lived objects compared to relatively few long-lived objects, the GC runs much more frequently to clean up objects in the lower generations than in the higher ones.



Arrays in C# .NET

The variables you have been working with so far have only been able to hold one value at a time. Your integer variables can only hold one number, and your strings one chunk of text. An array is a way to hold more than one variable at a time. Think of a lottery programme. If you’re just using single variables, you’d have to set up your lottery numbers like this:

lottery_number_1 = 1;
lottery_number_2 = 2;
lottery_number_3 = 3;
lottery_number_4 = 4;

Instead of doing that, an array allows you to use just one identifying name that refers to lots of numbers.

How to set up an Array

You set up an array like this:

int[ ] lottery_numbers;

So you start with the type of array you need. In the line above, we’re telling C# that the array will hold numbers (int). After the array type, you need a pair of square brackets. There should be no space between the array type and the first bracket. After the square brackets then type a space, followed by the name you want to use for your array,lotter_numbers in our case.

If your array needs to hold floating point numbers, you’d set your array up like this:

float[ ] my_float_values;

An array that needs to hold text would be set up like this:

string[ ] my_strings

So it’s pretty much just like setting up a normal variable, except you type a pair of square brackets after int, or float, or string.

The next thing you need to do is to tell C# how big your array will be. The size of an array is how many items it is going to hold. You do it like this:

lottery_numbers = new int[49];

So the name of your array goes before an equals sign ( = ). After the equals sign, you type the word new. This tells C# that it is a new object. After a space, you need the array type again (int for us). Next comes some square brackets. This time, however, you type the size of the array between the brackets. In the code above, we’re saying that the array will hold 49 numbers.

So the two lines would be:

int[ ] lottery_numbers;
lottery_numbers = new int[49];

If you prefer, you can put all that on one line:

int[ ] lottery_numbers = new int[49];

But you’re doing two things at once, here: before the equals sign, you’re telling C# that you want to set up an array; after the equals sign, you’re creating a new array object of a particular size.

Assigning values to your arrays

So far, you have just set up the array, and created an array object. But the array doesn’t yet hold any values. (Well it does, because C# assigns some default values for you. In the case of int arrays, this will be just zeros. But they’re not your values!)

To assign a value to an array, you use the square brackets again. Here’s the syntax:

array_name[ position_in_array ] = array_value;

So you start with the name of your array, followed by a pair of square brackets. In between the square brackets, you need a position in your array. You then type an equals sign, and the value that is going in that position. Here’s an example using our lottery numbers:

lottery_numbers[0] = 1;
lottery_numbers[1] = 2;
lottery_numbers[2] = 3;
lottery_numbers[3] = 4;

First of all, note that the first position in a C# array is zero, and not 1. This is slightly confusing, and can trip you up! But we’re telling C# to assign a value of 1 to the first position in the array, a value of 2 in the second position, a value of three in the third, and so on. Just bear in mind that array positions start at 0.

Another way to assign values in array is by using curly brackets. If you only have a few values going in to the array, you could set it up like this:

int[] lottery_numbers = new int[4] { 1, 2, 3, 4 };

So we’ve set up the array the same way as before – all on one line. This time, we have a pair of curly brackets at the end. In between the curly brackets, type the values for your array, and separate each value with a comma.

In the next lesson, you’ll see how to work with arrays and loops.



Delegates in C#……………………

A delegate in C# allows you to pass methods of one class to objects of other classes that can call those methods. You can pass method m in Class A, wrapped in a delegate, to class B and Class B will be able to call method m in class A. You can pass both static and instance methods. This concept is familiar to C++ developers who have used function pointers to pass functions as parameters to other methods in the same class or in another class. The concept of delegate was introduced in Visulal J++ and then carried over to C#. C# delegates are implemented in .Net framework as a class derived from System.Delegate. Use of delegate involves four steps.

1. Declare a delegate object with a signature that exactly matches the method signature that you are trying to encapsulate.
2. Define all the methods whose signatures match the signature of the delegate object that you have defined in step 1.
3. Create delegate object and plug in the methods that you want to encapsulate.
4. Call the encapsulated methods through the delegate object.



Using Namespaces…………

Let’s examine some code that uses the SomeUtility class I defined in the previous section:

public class EntryPoint


static void Main()


Acme.Utilities.SomeUtility util =

new Acme.Utilities.SomeUtility();



This practice of always qualifying names fully is rather verbose and might eventually lead to a bad case of carpal tunnel syndrome. The using namespace directive avoids this. It tells the compiler that you’re using an entire namespace in a compilation unit or another namespace. What the using keyword does is effectively import all of the names in the given namespace into the enclosing namespace, which could be the global namespace of the compilation unit. The following example demonstrates this:

using Acme.Utilities;

public class EntryPoint


static void Main()


SomeUtility util = new SomeUtility();



The code is now much easier to deal with and somewhat easier to read. The using directive, because it is at the global namespace level, imports the type names from Acme.Utilities into the global namespace. Sometimes when you import the names from multiple namespaces, you may still have naming conflicts if both imported namespaces contain types with the same name. In this case, you can import individual types from a namespace, creating a naming alias. This technique is available via namespace aliasing in C#. Let’s modify the usage of the SomeUtility class so that you alias only the SomeUtility class rather than everything inside the Acme.Utilities namespace:

namespace Acme.Utilities


class AnotherUtility() {}


using SomeUtility = Acme.Utilities.SomeUtility;

public class EntryPoint


static void Main()


SomeUtility util = new SomeUtility();

Acme.Utilities.AnotherUtility =

new Acme.Utilities.AnotherUtility();



In this code, the identifier SomeUtility is aliased as Acme.Utilities.SomeUtility. To prove the point, I augmented the Acme.Utilities namespace and added a new class named AnotherUtility. This class must be fully qualified in order for you to reference it, since no alias is declared for it. Incidentally, it’s perfectly valid to give the previous alias a different name than SomeUtility. Although giving the alias a different name may be useful when trying to resolve a naming conflict, it’s generally better to alias it using the same name as the original class name in order to avoid maintenance confusion in the future.



Assemblies and the Assembly Loader……..

An assembly is a discrete unit of reusable code within the CLR, similar in nature to a DLL in the unmanaged world, but that’s where the similarities end. An assembly can consist of multiple modules all linked together by a manifest, which describes the contents of the assembly. With respect to the operating system, a module is identical to a DLL. Assemblies can have a version attached to them so that multiple assemblies with the same name but different versions are identifiable separately.

Assemblies also contain metadata describing the contained types. When you distribute a native DLL, you typically include a header file and/or documentation describing the exported functions. Metadata fulfills this requirement, completely describing the types contained within the assembly. In short, assemblies are versioned, self-describing units of reuse within the CLR environment.

As discussed in the previous chapter, when you compile the “Hello World!” program, the result is an .exe file that is, in fact, an assembly. You can create managed assemblies using any managed language. Moreover, in most cases, any other managed language can consume managed assemblies.

Therefore, you can easily create complex systems developed with multiple managed languages. For example, when creating some low-level types, C++ /CLI may be the most natural languages to get the job done, but it may make more sense to code the top-level user interface using either C# or Visual Basic. To provide interoperability between the various languages, the CLI defines a subset of the type system known as the Common Language Specification (CLS). If you use only CLS-compliant types in your assemblies, you are guaranteed that any managed language can consume them.



Difference Between Data Hiding and Data Encapsulation…….

Data encapsulation is the process of combining and assembling elements in order to create a new entity and the wrapping of private data into a class. The encapsulation involves implementation concerning the data and the method. Data with internal methods are secured into a capsule while the methods are used to access the data inside it. In this manner, the assembly and grouping of all data and requirements inside a capsule makes it independent for an object to function together.

Data hiding has many uses which include:Security purposes and data protection from data misuse and unauthorized access. Utilizing changing functions to move with difficulty from the outside.Hiding the data unauthorized access or use from suspicious users, especially computer hackers who try to manipulate sensitive data or the program.Hiding the physical storage layout for the data.The programmer can avoid linking to incorrect data. If a programmer does link to the said data, the program will return an error to protect the content.



Debug vs. Release in .NET………….

Basically, debug mode is for you as a developer. It means that extra information
is contained in the executable (or dll) (for instance type info).
Compiling and linking your source code in debug mode allows you to break the
execution of your program using int 3 (interrupt 3). You can then step through
the code.
Obviously, all these goodies have a price. Your program will run much slower, and
it will be much bigger.

Release mode is for your customers. When you compile and link your program in
this mode your code will be optimized. Your program will run as fast as possible,
and will be much smaller.
Obviously, the price of the release mode is that you can’t (easily) break the
execution of your program to step through your code.



C# Dataset………..

The ADO.NET DataSet contains DataTableCollection and their DataRelationCollection . It represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables.

We can use Dataset in combination with DataAdapter Class . Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter. The DataSet object offers a disconnected data source architecture. The following link gives you in details about Dataset Class with C# Source Code .



%d bloggers like this: