Pages
Partial Classes
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
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
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#,
Nullableb = 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
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 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#
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 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#
Internally it is equivalent to
expression is type ? (type)expression : (type)null
consider a example,
object [] myobject = new object[5];output
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" );
}
0:not a string
1:'hello'
2:not a string
3:not a string
4:not a string