Pages

Showing posts with label C Sharp. Show all posts
Showing posts with label C Sharp. Show all posts

Partial Classes

Partial Classes allow user to split the class file accross multiple source files. The benifit behind this approach is to hide the funtionality of the class. the same class can be created in may places and appropriate methods can be placed in the code file so that it derived classes can focus on significant part alone.

Interfaces

Interface are a type similar to class, where it cannot have the Method Content, rather it has the definition of Method. It will not have properties and members variables. The class which inherits the interface should implement all the methods of the interface. The interface forces the derived class to implement; otherwise it will not get compiled.

The below lists some of common interfaces of .Net
1. IComparable - Implemented by types whose value can be ordered, it is used for sorting.
2. Idisposable - Defines this method to dispose of an object manually, this is important most commonly used Interface when dealing with more big objects, and also to release the object which holds the resource like database.
3. IConvertible - Enables the class to base type such as string, int or bool.
4. IClonable - Supports object copying
5. IEquatable - Allows comparing to object instances.
6. IFormatable - Provides way to convert the value of object into a formatted string. This is similar to ToString() method, but provides greater flexibility than it.

Interfaces

Interface are a type similar to class, where it cannot have the Method Content, rather it has the definition of Method. it will not have properies and members variables. the class which inherits the interface should implement all the methods of the interface. the interface forces the derived class to implement, otherwise it will not get compiled. The below lists ome of common interfaces of .Net
1. IComparable - Implemented by ttypes whose value can be ordered, it is ued for sorting.
2. Idisposable - Defines this method to dispose of a object manually, this is important most commonly used Interface when dealing with more big objects, and also to release the object which holds the resource like database.
3. IConvertible - Enables the class to to base type such as string, int or bool.
4. IClonable - Supports object copying
5. IEquatable - Allows to compare to object instances.
6. IFormatable - Provides way to convert the value of object into an formatted string. This is similar to ToString() method, but provides greater flexibility than it.

Nullable type in .Net 2.0

Nullable type allows a varible to store null value, Infact this is the special about the type. The main purpose is it will add two members to the variable HasValue and Value Members. for example, if you store data for a yes/no question and if the user did not answer the question then null will be stored in that place. This means it stores True, False and third state nothing as null also. This will be represented as shown below.

In VB.Net,
Dim b as Nullable(Of Boolean) = Nothing
.....
If b.HasValue Then
Do if b has some value and not null
Else
Do if b is null
End If
In C#,
Nullable b = null;
Otherwise it can be used this way also, only applicable for C#,
bool? b = null;
.....
if (b.HasValue) {Do if it is not null} else {Do if it null}

Optimizing performance with built-in types .Net

The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables. For floating-point operations, Double is the most efficient type because those operations are optimized by hardware.

Readonly Vs Constant Variables in .Net

ReadOnly variable can be declared without assigning a value to it.
Constant variable can only be declared with assigning a value to it.

ReadOnly variable can be assigned in constructor or in object intialization.
Constant are same, cannot be modified anytime.

Dispose Vs Finalizer

The Dispose and Finalizer are methods used to clear the resources used for the object.
The Dispose is a method of IDisposable interface. we have to inherit this interface for our object and implement the method Dispose, to clear our objects used in our application. suppose if we use collection and dictionary or any other huge resource for a specific object, we have to clear all the memory used by the object when it is destroyed. to make this happen internallly for every object we have to implement Dispose method and it takes care of the clearing the memory.
The Finalizer method is also similar type but this will be called when GC gets invoked. this GC.Collect call will invoke Finalizer method for each and every object. this will do a emergency clean, it may lead to data loss some times.

we can also implement finalizer method as a special case. the call of finalizer method after a dipose is a waste of process time, we can bypass this action by setting the property SupressFinalizer to true. this will help to avoid calling the finalizer method when GC gets activated.

New operator ?? in c#

The operator '??' is in c#, which is used check the value is null.
Previously, we used to check the null value by the following methods..

if (a!=null) { c=a; } or
c = ((a!=null)?a:0);

we can do the same checking with the new operator ?? like this..
c = a??0;

By this statement the value a will be assigned to c if it is not null else the zero will be assigned.

More on DataTable in ADO.Net 2.0

The DataTable's CreateDataReader method creates an instance of DataTable Reader. When a DataTableReader is created from a DataSet or a DataTable's CreateDataReader method, the DataTableReader will contain all of the rows from the container object, with the exception of deleted rows.
The DataTableReader is a lighter weight object than the DataTable and, unlike the DataReader (SqlDataReader), the DataTableReader is disconnected. This is a great feature because you get the lightweight object that you can iterate through quickly (like the DataReader) and it is disconnected from any data source (unlike the DataReader). A DataTableReader can be thought of as an iterator over the underlying table's rows, similar to a foreach enumeration of the contents of the table.Keep in mind that the DataTableReader only moves forward.
The following example shows how you can create a DataTableReader.
using (SqlConnection cn = new SqlConnection(cnStr))
{
SqlCommand cmd = new SqlCommand(sqlAllCustomers, cn);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataTable dtCustomers = new DataTable("Customers");
adpt.Fill(dtCustomers);
DataTableReader dtRdr = ds.CreateDataReader();
dgvCustomers.DataSource = dtRdr;
}

Loading multiple Table's in DataTableReader is similar to the retriving multiple resultset. the method NextResult can be used to move to other table's records.
DataTableReader dtRdr = dt1.CreateDataReader();
DataTable dt2 = new DataTable();
dt2.Load(dtRdr);

Like Dataset Merge, Two Tables can be merged together by the method Merge of DataTable.
dtTableOne.Merge(dtTableTwo);

Similar to DataSet, DataTable can be written as XML by the WriteXml method. the DataTable in ADO.NET 2.0 also exposes the ReadXml, ReadXmlSchema, and WriteXmlSchema methods.
The DataSet also has some new methods and properties. In fact, both the DataSet and the DataTable now expose the RemotingFormat property as well as the Load and CreateDataReader methods. The RemotingFormat property is used to indicate whether to serialize the DataTable or DataSet in binary or XML format.

as operator in C#

The as operator is like a cast. it only performs the reference conversions and boxing convertions, the conversion of user defined is not possible, which has to done by casting explicitly. usage of 'as' operator never raises a exception instead it yields null on conversion failure.

Internally it is equivalent to
expression is type ? (type)expression : (type)null


consider a example,


object [] myobject = new object[5];
myObjects[0] =
new MyClass2();
myObjects[1] = "hello";
myObjects[2] = 123;
myObjects[3] = 123.4;
myObjects[4] = null;
......
for (int i=0;
i<myObjects.Length; ++i)
{

string s = myObjects[i] as string;
Console.Write ("{0}:", i);
if (s != null)
Console.WriteLine ( "'" + s + "'" );
else

Console.WriteLine ( "not a string" );
}


output


0:not a string
1:'hello'
2:not a string
3:not a string
4:not a string