Thursday 30 August 2012

Partial Type in C#

Partial type is a new feature introduced in .Net 2.0 (VS2005). Using Partial type you can define a single class in multiple files (more than one files) at compilation time these classes are combined into a single class. You can apply partial modifier on a class or a struct, or an interface.

Partial modifier is not available on delegate or enumeration declarations.

There are a few things that you should be careful about when writing code for partial classes:

·         All the partial types must be defined within a same assembly and module.
·         Method signatures (return type, name of the method, and parameters) must be unique for the aggregated typed (which was defined partially).
·         The partial types must have the same accessibility.
·         Inheritance at any partial type applies to the entire class.
·         If any part is sealed, the entire class is sealed.
·         If any part is abstract, the entire class is abstract.

Example

//partial class
public partial class Employee
{
public virtual void GetEmployeeNo();
}

public partial class Employee
{
public virtual void GetEmployeeName();
}

//Derived class
public class Department : Employee
{
       public void getEmployeeDetails()
       {
     GetEmployeeNo ();
     GetEmployeeName ();
       }
}

Saturday 25 August 2012

Difference between IEnumerable, ICollection, IList, and IDictionary


Summary/Definitions:

All the below interfaces are parts of the System.Collections .Net assembly.

IEnumerable: Exposes the enumerator, which supports a simple iteration over a non-generic collection.

ICollection: Defines size, enumerators, and synchronization methods for all non-generic collections

IList: Represents a non-generic collection of objects that can be individually accessed by index.

IDictionary: Represents a non-generic collection of key/value pairs.

Following is the definitions of interfaces.

public interface IEnumerable

public interface ICollection : IEnumerable

public interface IList : ICollection, IEnumerable

public interface IDictionary : ICollection, IEnumerable

Differences between above assemblies are as follow.

Interface
Method(s)
Propertie(s)
IEnumerable
GetEnuerator()which will Returns an enumerator that iterates through a collection
-
ICollection
CopyTo(System.Array, int) method which will Copies the elements of the System.Collections.ICollection to an System.Array, starting at a particular System.Array index
Count (Gets the number of elements contained in the System.Collections.ICollection).
IsSynchronized (Gets a value indicating whether access to the System.Collections.ICollection is synchronized (thread safe)).
SyncRoot (Gets an object that can be used to synchronize access to the System.Collections.ICollection).
IList
Add(object) Adds an item to the System.Collections.IList, Clear()Removes all items from the System.Collections.IList.
IsFixedSize Gets a value indicating whether the System.Collections.IList has a fixed size.
Contains(object) Determines whether the System.Collections.IList contains a specific value.
IsReadOnly Gets a value indicating whether the System.Collections.IList is read-only.
IndexOf() Determines the index of a specific item in the System.Collections.IList
This[int] Gets or sets the element at the specified index.
Insert(int,object) Inserts an item to the System.Collections.IList at the specified index.
Remove(object) Removes the first occurrence of a specific object from the System.Collections.IList.
RemoveAt(int) Removes the System.Collections.
IDictionary
Add(object,object) Adds an element with the provided key and value to the System.Collections.IDictionary object.
IsFixedSize Gets a value indicating whether the System.Collections.IDictionary object has a fixed size.
Clear() Removes all elements from the System.Collections.IDictionary object.
IsReadOnly Gets a value indicating whether the System.Collections.IDictionary object is read-only.
Contains(object) Determines whether the System.Collections.IDictionary object contains an element with the specified key.
Keys Gets an System.Collections.ICollection object containing the keys of the System.Collections.IDictionary object.
GetEnuerator()Returns an System.Collections.IDictionaryEnumerator object for the System.Collections.IDictionary object.
This[object] Gets or sets the element with the specified key.
Remove(object) Removes the element with the specified key from the System.Collections.IDictionary object.
Values Gets a System.Collections.ICollection object containing the values in the System.Collections.IDictionary object.

So from above table we can conclude that IEnumerable interface is read-only collection. We can iterate through IEnumerable collection using yield also. 

ICollection is also read-only collection but we can copy collection into array and we have count, IsSyncronized etc. readonly properties on it. 

IList is collection where we can add, remove, search, and find data from collection with additional properties like IsFixedSize, IsReadonly etc. 

Where as IDictionary interface works same as IList but instead of integer index we have key, value pairs.

How to clear clickonce deployment cache

As per standard development process we are hosting application after new Iteration (Aglie Methodology) or module development. For client testing we are hosting new application version after every release on hosting server.

Here we come to an issue where our application hosting newer version referring to the assemblies of old application hosting assemblies version. This was creating an issue with application as we have fixed defects are still appearing on newer version of assemblies!!!! ????. This is a standard problem with clickonce deployment. To overcome this issue we can do the following steps.
  1.       Go to start windows à Run menu.
  2.       Run the following command rundll32 dfshim CleanOnlineAppCache

OracleDataReader FetchSize Property

Our application loads a big cache of data from database when it starts. A DataReader is an option we have chossen to retrive the date, since it’s a set of read-only, forward-only operations.

I observed that to load an Oracle 11g table with 2 million rows and 20 columns the performance increased a lot by reducing the number of round-trips to database while using DataReader.

Following is the example of Employee Table which contains huge number of records.

var connection = new OracleConnection(myConnectionString); 
const string sql = @"SELECT * FROM Employee"
var command = new OracleCommand(sql, connection)
{
       CommandType = CommandType.Text
};

var dataTable = new DataTable("Employee");

using (var oracleReader = command.ExecuteReader())
{
oracleReader.FetchSize = (1000 * command.RowSize);
         dataTable.Load(oracleReader);
}

return dataTable;

Thursday 23 August 2012

Difference between Inheritance and Composition

They are absolutely different. Inheritance is an "is-a" & Composition is a "has-a" relationship.

In inheritance the superclass is created when the subclass is created. In Composition, the object is created when the coder wants it to. This is inheritance, when the Child class is created the parent is created because the child inherits from parent.

class Parent { 
    //Some code
}

class Child : Parent{ 
    //Some code
}

This is composition, the object is not created when the child class is created, instead it is created whenever it is need.

class Parent{
    //Some code
}

class Child{
    private Parent parent = new Parent();
    //Some code
}

In this case, the Parent class is also created when the Child class is created. Below is another example of Composition without the object being created when the child class is created

class Parent{
    //Some code
}

class Child{
    private Parent parent;
    public Child()
    {
    }
    public void createParent()
    {
         parent = new Parent();
    }
}

Note how the parent is not created until a call is made to the createParent.

Wednesday 22 August 2012

What is predicate delegate in C#


A predicate is a function that returns Boolean true or false. A predicate delegate is a reference to a predicate.A predicate delegate is a delegate with the following signature:
  •          Return type - bool
  •          Argument type - generic
A predicate delegate thus is a delegate which is capable of taking any custom type as an argument. Predicates are very useful for filtering a list of values - here is an example.

using System;
using System.Collections.Generic;

public class Program
    {
        private static void Main(string[] args)
        {
            var list = new List<string> { "abc",  "xyz""pqr"};
            var predicate = new Predicate<string>(EqualToAbc);
            var newList = list.FindAll(predicate);
        } 
        private static bool EqualToAbc(string arg)
        {
            return arg.Equals("abc");
        }
    }

Now if you are using C # 3 Or Later versions you can use a lambda to represent the predicate in a cleaner fashion:

using System;
using System.Collections.Generic;

public class Program
    {
        private static void Main(string[] args)
        {
            var list = new List<string> { "abc",  "xyz""pqr"};          
            var newList = list.FindAll(i => i.Equals(("abc");
        }    
    }

Custome Service Class to connect wcf service

  using  System; using  System.ServiceModel; namespace Custom.Service.Client {      // Borrowed from: http://old.iserviceoriented.com/blog/p...