Archive

Archive for the ‘C#’ Category

Hashtable in C#

July 22, 2015 Leave a comment

Hashtable objects are quite similar to an ArrayList object except that it is not required to use a numerical index. Instead, we can use a texture key that can be
numeric.

Benefits of Hashtable

  1. Insert Elements: you can add as many pairs of key/value elements as required. You do not have to specify the size ahead of time.
  2. Non-numeric index: you can use text, number and dates as your key (index).
  3. Faster Lookup: the hashtable collection caters very fast lookup of elements.
  4. Flexibility when Removing elements: you can remove any element from an Arraylist very easily

Limitations of Hashtable

  • Key must be unique: The Hashtable stipulates the key uniqueness requirement that is very cumbersome to manipulate.
  • No useful sorting: Sorting is not done by using keys or values. The items in the Hashtable are sorted internally to make it easy to find objects very quickly.
  • Performance: Although the lookup is very quick in a Hashtable, the CLR must do quite a lot of work to maintain them that is very resource-intensive.

Hashtable obj = new Hashtable();
Obj[“in”] = “india”;
Obj[“en”] = “England”;
Obj[“us”] = “USA”;

Obj[Convert.ToDateTime(“21/12/2012”)] = “The Judgment day”;

string Country = Convert.ToString(obj[“in”]);

Advertisements

ArrayList in C#

July 22, 2015 Leave a comment

Benefits of Array List

  1. Insert Elements: An Array List starts with a collection containing no elements. You can add them in any position as you choose them.
  2. Automatic Resizing: you do not need to specify the array size; as you add elements, the array automatically ensures there is enough memory for the array.
  3. Flexibility When Removing Elements: you can remove any element from an Array list very easily.

Limitations of Array List

The flexibility of Array List comes at a cost of performance. Since memory allocation is a very expensive business, the fixed number of elements of the simple array
makes it much faster to work with.

Example:

ArrayList obj = new ArrayList();
obj.Add(“item1”);
obj.Add(“2”);
obj.Add(“Delhi”);

Obj.Insert(2,”item2″);

Obj.Remove(“item1”);
Obj.RemoveAt(3);

Abstract Class features

July 20, 2015 Leave a comment

  • class members that are incomplete and must be implemented in a derived class.
  • An abstract class cannot be instantiated.
  • The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
  • Abstract classes may also define abstract methods.


public abstract class A
{
public abstract void DoWork(int i);
}

  • Derived classes of the abstract class must implement all abstract methods.
  • When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.
  • We can also inherit normal method in the abstract class and declare it as abstract, though it compiles it shows warning that abstract method must be virtual.


public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}


public abstract class E : D
{
public abstract override void DoWork(int i);
}


public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}

  • If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class.
  • A class inheriting an abstract method cannot access the original implementation of the method.
  • override keyword  should be used while defining abstract method in derived class.

Ref:- http://msdn.microsoft.com/en-us/library/yd3z1377.aspx

Default Access Modifiers in C#

September 23, 2013 Leave a comment

Non Nested type 

                    | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | internal  | public, internal

class                | internal  | public, internal

struct               | internal  | public, internal

delegate             | internal  | public, internal

Nested Type
                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | public    | none

class                | private   | All¹

struct               | private   | public, internal, private²

delegate             | private   | All¹

constructor          | protected | All¹

interface member     | public    | none (always implicitly public)

method               | private   | All¹

field                | private   | All¹

user-defined operator| none      | public (must be declared public)

¹ All === public, protected, internal, private, protected internal

² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

 
Categories: C#

Nullable type

September 10, 2013 Leave a comment

Normally we are expert in using value types like int, bool, decimal types.But those types dont have null values by default. Suppouse if you want to insert null value to database for above value types.

Example:

Boolean variable can have two values: true and false. There is no value that signifies “undefined” (null). Suppose you want to insert null value in the database column for Boolean value then there is no direct option to store null value since i have only two values: true and false.

bool boolIsNull= null; // Not Possible

To handle this kind of values .NET give us a very useful tool to for this: nullable types.

Nullable<T> //Nullable<int> or Nullable<decimal>
bool? boolIsNull= null; //Correct

 

Ref: http://www.codeproject.com/Articles/275471/Nullable-Types-in-Csharp-Net


								
Categories: C#

Difference between Class and Structure

March 6, 2013 Leave a comment

Classes are Reference types and Structures are Values types.

Classes are reference types, basically they will contain the address of an instance variables.

Class SimpleClass
{
Public int Value; 	
}

This the simple class am assigning value to Object to it.

Static Public void Main (string [] arg)
{
SimpleClass objSimpleClass1=new SimpleClass();
objSimpleClass1.Value=5;
SimpleClass objSimpleClass2= new SimpeClass();
objSimpleClass2.Value=10;
}

objSimpleClass1.Value=5

after in next step we change it to 10 in the object objSimpleClass2.  Since both object refer the same address, changes happened in the objSimpleClass2 or in last step that value will reflect in other object also.


								
Categories: C#

What is Boxing and UnBoxing

March 5, 2013 Leave a comment

Converting a value type to reference type is called Boxing.

Example:

int i = 12;
object box = i;

Converting a reference type to value type is called UnBoxing.

Example:

object box = 12;
int i = (int)box;
%d bloggers like this: