Thursday, September 30, 2010

Abstract and Sealed concepts

Abstract classes and members

Abstract keyword used to define class and class members for the purpose of inheritance.
An abstract class can't be instantiated and an abstract method doesn't have definition in base class, declaration followed by semicolon.

public abstract classA
{
public abstract void dowork(int i); //Abstract method has only declaration in base class
}

A non abstract derived class of an abstract base class must define all the abstract methods of base class and inherited abstract members from upper hierarchy of base class.
public classB : classA
{
public override void dowork(int i)
{
//Implementation of abstract method of base class
}
}

A. Should not define public or protected internal constructor.
B. Can be define Protected or internal constructor.
C. Abstract members are implicitly Virtual.
D. Declaring an abstract member as Virtual or Static is an error.
E. Abstract class may contain non abstract members, non abstract members can have definition in abstract class, meant common method for all derived classes without overriding the method.
F. An abstract derived class can define or not define abstract members of base class.
G. An abstract class can't be declared as Sealed, because having opposite meaning.
H. Non abstract class can't contain abstract members, abstract method can be declared in Abstract class.
I.  An abstract method declaration don't have braces {}, declaration followed by semicolon only.
    Ex.  public abstract void dowork(int i);
J. An Abstract class must implement all members of inherited Interface but not required to override all abstract members of inherited base class.
k. Abstract method declaration is allowed to override a virtual method of base class.

class A
{
   public virtual void F() {
      Console.WriteLine("A.F");
   }
}
abstract class B: A
{
   public abstract override void F();
}
class C: B
{
   public override void F() {
      Console.WriteLine("C.F");
   }
}

Purpose of abstract class to make common definition of a base class that multiple derived classes can share.
A class library has a abstract class than programmers can use the library and have their own implementation of derived class using base abstract class.

Ex.
     public abstract class Animals
    {
        public void Move()  //Non Abstract function
        {
            string s = "All animals can move";
        }
        public abstract void Breath();   
    }
    public class Dogs : Animals
    {
        public Dogs() { }
        public override void Breath()
        {  //Breath in open air
        }
    }
    public class Fish : Animals
    {
        public Fish() { }
        public override void Breath()
        {  //Breath in water
        }
    }
Dogs DaburMan = new Dogs();
            DaburMan.Breath();   //Abstract function of base abstract class
            DaburMan.Move();     //Non Abstract function of base abstract class
       Fish Shark = new Fish(); 
            Shark.Breath();  //Abstract function of base abstract class
            Shark.Move();    //Non Abstract function of base abstract class

***********************

Sealed classes and members


A sealed class can't be inherited, Sealed keyword is used to protect from derivation a class.
A sealed class can't be abstract, because of opposite meaning.
A sealed method also can't be override in derived class.
When applied to method or property sealed keyword always used with override keyword.

If you override an base class virtual method in derived class, you can declare that sealed there(on override) to protect override of that method in further derived classes. This negates the virtual aspect of the member for any further derived class.

public class D : C
{
public sealed override void DoWork()
{
// Implementation
}
}

Scenario - Creating Sealed class
1. To take away inheritance feature from users so they can't inherit that class.
2. If we have some internal operation/s in class or function, because access of class or function in this case can be harmful. ex. .net's string class is sealed.
3. If we have static members in class(Static members are sealed by default). ex pen, brush classes are sealed.

Saturday, September 25, 2010

What's new in the .NET framework version 3.5

  1. Framework - 3.5 extends many new features like Linq, WCF those are useful to develop pocket PC, smart phone and distributed mobile applications.
  2. ASP.Net -
    1. 3.5 includes AJAX while in 2.0 it needs to install. Microsoft AJAX library supports client-centric, object orient development which is browser independent. ScriptManager to make a page AJAX enable. UpdatePanel to target a specific area of page rather than refreshing whole page.   
    2. Visual web developer includes improved intellisense for java script and Ajax library.
    3. Listview control displays data provided by data source by using user defined templates. User can select, insert, update, delete and sort records. Listview includes features of data grid, grid view, data repeater and other similar list controls available in 2.0.
    4. Data pager provides paging functionality to data bound controls those implements ‘Ipageableitemcontainer’ interface like listview control.
    5. LinqDataSource for Linq(Language integrated query).
    6. ASP.Net Merge Tool to merge precompiled assemblies (aspnet_merge.exe) .
  1. Add-Ins and Extensibility - 3.5 introduces new architecture and model that enables powerful and flexible support to developers to develop extensible applications.

  2. CLR – 3.5 has improved CLR. Also has improvements in collection, garbage collection, better reader and writer locks in threading, i/o and pipes, reflection, diagnostics etc.

  3. Cryptography – 3.5 includes improved features for security. Some new cryptography classes added those used to verify and obtain information of manifest signature. 3.5 supports suite B cryptography algorithms published by National Security Agency(NSA).

  4. Networking Several features of networking get improved in 3.5. Peer to peer networking is server less networking technology allows network devices to share resources and communicate with each other directly, is get improved in 3.5.

  5. LINQ Its a new feature in 3.5 extends powerful query capabilities to language syntax of C#, VB.Net. Its standard, easy to understand and learn. 3.5 extends some assemblies those enable the use of Linq to query .net collections, databases, xml documents, ado.net data sets.
    System.Linq – (exist in System.core.dll) Extends standard operators and types for Linq.
    System.Data.Linq – Enables interaction with databases.
    System.Data.Linq.Mapping – Enables to generate Linq to Sql object model.
    System.xml.Linq – Enables Linq to access and modify xml documents.

  6. Expression trees – Expression trees are new in .net 3.5, it provides a way to present language level code in the form of data.

Thursday, September 2, 2010

SQL SERVER - Sql Server2000 Vs Sql Server 2005

  1. In 2005 enterprise manager and query analyzer given together, you can also open multiple windows in same enterprise manager.
  2. In 2005 you can create 2(pow(20)-1) databases while in 2000 its 65,535.
  3. 2005 introduced some new data types like ‘xml’.
  4. In 2005 you can store large value and object type by using ‘max’ like varchar(max), nvarchar(max), varbinary(max).
  5. In 2005 you can write try – catch statements in stored procedure.

    NSRI (shortcut to remember) in Sql Server 2005 –

    Notification services enhancement
    Service broker
    Reporting service enhancement
    Integration service enhancement

Delegate

A delegate is a type that defines a method signature. When you create an instance of the delegate, you can associate a method to the instance of compatible signature, now you can call or invoke that method by the delegate instance.
Delegates are used to pass a method as argument to other method. A delegate instance can be assigned a method of any accessible class or struct that matches signature of delegate, method can be static or instance method.
Ex.
Public Delegate int myDelegate(int x, int y); //Delegate declaration
Public int add(int a, int b) //Method definition
{
Return a+b;
}
myDelegate del = add;  //Delegate instance del assigned method ‘add’
                                   //(Equivalent to  myDelegate del = new myDelegate(add); )

del(12, 30); //call or invoke

General terms
1. Delegates are like function pointers in C or C++.
2. Delegates can be used to pass a method as argument to other method.
3. Delegates can be chained together, multiple methods can be called on a single event. When you call a delegate that is chained(added multiple delegates) then all the methods called on this single call this is called Multicasting or Chaining of delegates. All methods called in the sequence in which they added. 
4. Covariance allows a more derived return type method to be assign than delegate’s return type.
5. Contravariance allows less derived parameters type method to be assign than delegate’s parameter’s type.
6. Framework 2.0 introduced anonymous methods, which allows code blocks to be passed to delegate as parameter in place of separately defined method.
Code block may contain anonymous method or lambda expression. 

//Instantition of myDelegate with instance mdel assigned anonymous method
myDelegate mdel = delegate(int m, int n) {
//Code block
}
//Instantition of myDelegate with lambda expression
(input parameters) => expression //lambda expression


myDelegate mdel =  (int x, int y) => { Code block }
OR
myDelegate mdel =  (x, y) => { Code block }  //Compiler infer input types by own

mdel(20,100); //Call or invoke
7. Delegates types ex. del (instance of delegate - myDelegate del; ) are drived from Delegate class, delegate types are sealed they can not be derived from.
8. When you invoke delegate, parameters passed to the delegate, pass to the method and returns value (if have return type) through delegate, its not possible to create custom class from Delegate class.
9. Instance of delegate is an object so it can be assign to a property or can be pass to a method as parameter and call the delegate at some later time this is called
Asynchronous callback.
10. Delegate can be assign static method.
11. A delegate can be chained (multicast) with static and instance both type methods together.


In C# 1.0 and later, delegates can be declared as shown in the following example.
// Declare a delegate.
delegate void Del(string str);

// Declare a method with the same signature as the delegate.
static void Notify(string name)
{
    Console.WriteLine("Notification received for: {0}", name);
}
// Create an instance of the delegate.
Del del1 = new Del(Notify);

C# 2.0 provides a simpler way to write the previous declaration, as shown in the following example.
// C# 2.0 provides a simpler way to declare an instance of Del.
Del del2 = Notify;

In C# 2.0 and later, it is also possible to use an anonymous method to declare and initialize a delegate, as shown in the following example.
// Instantiate Del by using an anonymous method.
Del del3 = delegate(string name)
    { Console.WriteLine("Notification received for: {0}", name); };

In C# 3.0 and later, delegates can also be declared and instantiated by using a lambda expression, as shown in the following example.
// Instantiate Del by using a lambda expression.
Del del4 = name =>  { Console.WriteLine("Notification received for: {0}", name); };

Combining or multicasting of delegates
Public Delegate void Del(String s);
Class Test
{
Static void hello(string s)
{
System.Console.WriteLine(“Hello {0}!”,s);
}

Static void Goodbye(string s)
{
System.Console.WriteLine(“Good Bye {0}!”,s);
}

Static void main()
{
Del a, b, c, d;
a = hello; //Assigning hello method to delegate a

b = Goodbye; //Assigning hello method to delegate a

c = a + b; // ( OR c = a; c += b;)

d = c – b; // ( OR d = c; d -= b;)
a(“Ram”); // Invoke delegate a
b(“Shyam”); // Invoke delegate b
c(“Ravi”); // Invoke delegates a then b
d(“Shubham”); // Invoke delegate a
}
}
Output
Hello Ram! //result of a
Good Bye Shyam! //result of b
Hello Ravi!     //result of c
Good Bye Ravi!
Hello Shubham! //result of d

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...