22 June 2011

Reflection in C#

Reflection in C#

Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.
// Using GetType to obtain type information: int i = 42;
System.Type type = i.GetType(); System.Console.WriteLine(type);
The output is:

System.Int32

// Using Reflection to get information from an Assembly:
System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");
System.Console.WriteLine(o.GetName());

The output is:

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089


20 June 2011

Example for Array list in C# and Binary Search,Sort,Remove,Removeat

ArrayList Class

Implements the IList interface using an array whose size is dynamically increased as required.
The capacity of a ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as
required through reallocation.
//Example for Array list in C# and Binary Search,Sort,Remove,Removeat

using System;
using System.Collections;
public class SamplesArrayList
{

public static void Main()
{

ArrayList myAL = new ArrayList();
//Insert values in arraylist
for (int i = 0; i <= 10; i++)
myAL.Add(i * 2);

// Displays the ArrayList.
Console.WriteLine("The Int32 ArrayList contains the following:");
PrintValues(myAL);

// Locates a specific object that does not exist in the ArrayList.
Object myObject = 4;
FindMyObject(myAL, myObject);

// Locates an object that exists in the ArrayList.
Object myObjectSearch = 11;
FindMyObject(myAL, myObjectSearch);

// Removes the element containing "4".
myAL.Remove(4);

// Removes the element at index 5.
myAL.RemoveAt(5);

// Removes three elements starting at index 4.
myAL.RemoveRange(4, 3);

// Sorts the values of the ArrayList.
myAL.Sort();

//Print the Values
PrintValues(myAL);
}

///
/// Find the value in the array List
///
///
///
public static void FindMyObject(ArrayList myList, Object myObject)
{
//Store the index value of the search value
int myIndex = myList.BinarySearch(myObject);
//Check the index exist or not
if (myIndex < 0)
Console.WriteLine("The object to search for ({0}) is not found.", myObject);
else
Console.WriteLine("The object to search for ({0}) is at index {1}.", myObject, myIndex);
}

///
/// Print the array list
///
///
public static void PrintValues(IEnumerable myList)
{
foreach (Object obj in myList)
Console.Write(" {0}", obj);
Console.WriteLine();
}

}


Choose the right collection type for your needs in dotnet framework

Choose the right collection type for your needs,

-when you are not concerned about the order of the items in the collections, your first inclination should be to use a System.Collections.Generic.Dictionary

-The three basic operations (Add, Remove, and Contains) all operate quickly even if the collection contains millions of items.

-Using the new LinkedList collection can potentially help you improve performance in scenarios where you need to maintain order yet still achieve fast inserts.

-Unlike List, LinkedList is implemented as a chain of dynamically allocated objects.

-The downside of a linked list from a performance point of view is increased activity by the garbage collector as it has to traverse the entire list to make sure objects were not disposed of.
-Inserting an item into a LinkedList is much faster than doing so into a List

-Dictionary may be most appropriate. However, lookup is only fast in the average case, and certain datasets could cause that performance to degrade quickly.

-SortedDictionary, uses a balanced tree implementation as the underlying data store; this provides relatively fast lookups and maintains items in a sorted order but insertions will most likely be slower (and will vary based on the number of items in the collection)

-Alternatively, you can also use SortedList, which uses two separate arrays to maintain the keys and the values separately and to maintain them both in order (in the worse case you would have to shift all the keys and values).


Custom Collections

-In certain situations, you may not find that all of your requirements are implemented in any of the existing collection types in the Framework.

-It may be that you have a unique set of requirements or that you can still use an existing collection with minor changes.

If you decide you want to write a custom collection, you should first look into extending an existing collection type.

System.Collections.ObjectModel namespace.

These collections already implement the most needed interfaces and give you the baseline functionality you expect.


17 June 2011

Difference between namespace and assembly

Difference between namespace and assembly

Assembly
An Assembly is a logical unit of functionality that runs under the control of the .NET common language runtime (CLR). An assembly physically exists as a .dll file or as an .exe file.

An Assembly can contains one or many namespaces

Namespace
Namespaces are a way of grouping type names and reducing the chance of name collisions.

16 June 2011

15 June 2011

View in Sql Server 2008

View in Sql Server 2008

Creates a virtual table whose contents (columns and rows) are defined by a query. Use this statement to create a view of the data in one or more tables in the database.
For example, a view can be used for the following purposes:

-To focus, simplify, and customize the perception each user has of the database.

-As a security mechanism by allowing users to access data through the view, without granting the users permissions to directly access the underlying base tables.

-To provide a backward compatible interface to emulate a table whose schema has changed.

Example
--Partitioned view as defined on Server1
CREATE VIEW Customers
AS
--Select from local member table.
SELECT *
FROM CompanyData.dbo.Customers_33
UNION ALL
--Select from member table on Server2.
SELECT *
FROM Server2.CompanyData.dbo.Customers_66
UNION ALL
--Select from mmeber table on Server3.
SELECT *
FROM Server3.CompanyData.dbo.Customers_99

14 June 2011

Interface vs Abstract Class in C#

Interface vs Abstract Class

Multiple inheritance

Interfaces:A class may inherit several interfaces.
Abstract Class:A class may inherit only one abstract class.

Default implementation

Interfaces:An interface cannot provide any code, just the signature.
Abstract Class:An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modifiers

Interfaces:An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
Abstract Class:An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces:Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
Abstract Class:An abstract class defines the core identity of a class and there it is used for objects of the same type.

Usage in Application

Interfaces:If various implementations only share method signatures then it is better to use Interfaces
Abstract Class:If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Interfaces:Requires more time to find the actual method in the corresponding classes.
Abstract Class:Compare to Interface it is very fast

Fields and Constants

Interfaces:No fields can be defined in interfaces
Abstract Class:An abstract class can have fields and constrants defined


Main Limitation of Interface

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method
in all derived class.

Main Limitation of Abstract Class

A class may inherit only one abstract class.

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...