Friday, January 9, 2015

Difference between Array and Arraylist

Arrays
Arrays are strongly typed collection of same datatype and these arrays are fixed length that cannot be changed during runtime. Generally in arrays we will store values with index basis that will start with zero. If we want to access values from arrays we need to pass index values.
Declaration of Arrays
Generally we will declare arrays with fixed length and store values like as shown below
string[] arr=new string[2];
arr[0] = "welcome";
arr[1] = "Aspdotnet-suresh";
In above code I declared array size 2 that means we can store only 2 string values in array.
Arraylists

Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace
Declaration of Arraylist
To know how to declare and store values in array lists check below code
ArrayList strarr = new ArrayList();
strarr.Add("welcome"); // Add string values
strarr.Add(10);   // Add integer values
strarr.Add(10.05); // Add float values
If you observe above code I haven’t mentioned any size in array list we can add all the required data there is no size limit and we will use add method to bind values to array list.
Difference between Array and ArrayList
Arrays
ArrayLists
These are strong type collection and allow to store fixed length
Array Lists are not strong type collection and size will increase or decrease dynamically
In arrays we can store only one datatype either int, string, char etc…
In arraylist we can store all the datatype values
Arrays belong to System.Array namespace
Arraylist belongs to System.Collection namespaces


http://www.aspdotnet-suresh.com/2013/09/difference-bw-array-and-arraylist-in-csharp-example.html 

Wednesday, January 7, 2015

What is the role of inetinfo.exe, aspnet_isapi.dll and aspnet_wp.exe?

Why .Net does not support multiple inheritance?


Let me tell you a scenario.

I have a class Shape1

It has CalcualteArea method
Class Shape1
{
     public void CalculateArea()
     {
       //
     }
}

There is another class shape2 that one also has same method 
Class Shape2
{
     public void CalculateArea()
     {
       //
     }
}

Now i have a child class Circle, it derives from both SHape1 and shape2;

public class Circle: Shape1,shape2
{
}

Now when i create object for Circle, and call the method, system doesn't know which calculate area method to be called.. Both has same signatures. So compiler will get confuse .that's why multiple inheritances are not allowed.

But there can be multiple interfaces because interfaces dont ve methjod definition..Even both the interfaces have same method, both of them dont ve any implementation and always method in the child class will be executed..

------------------------------------------------------
To understand that you need to understand the Diamond problem first:

The Diamond problem is an ambiguity that arises when two classes B and C inherit from Class A and class D inherits from both B and C. If a method in D calls a method defined in A(and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B or C?

That is the reason why C# and Java languages does not support multiple inheritance.

A diamond class inheritance diagram.



The "diamond problem" (sometimes referred to as the "deadly diamond of death"[5]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
For example, in the context of GUI software development, a class Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in Rectangle or Clickable (or both), which method should be eventually called?
It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this case, class A is at the top, both B and C separately beneath it, and D joins the two together at the bottom to form a diamond shape

http://en.wikipedia.org/wiki/Diamond_problem#The_diamond_problem
http://www.artima.com/intv/dotnetP.html 

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx 

Monday, December 29, 2014

ASP.NET Caching


http handler, http modules


Sunday, December 28, 2014

Method Overloading



public static void Calculate(int a, double b)
        {
            Console.WriteLine("int,double");
        }
        public static void Calculate(double a, int b)
        {
            Console.WriteLine("double,int");
        }
static void Main(string[] args)
        {

            /// Error-The best overloaded method match for '.Calculate(double, int)' has some invalid arguments.
            /// Error-Argument 2: cannot convert from 'double' to 'float'
            ///Calculate(2.5, 2.5);/// Invalid



///Error-The call is ambiguous between the following methods or properties: 'Calculate(int, double)' and 'Calculate(double, int)'
            ///Calculate(2, 2);/// Invalid
            Calculate(2, 2.5);/// Valid
            Calculate(2, 2.5f);/// Valid
            Calculate(2.5, 2);/// Valid
            Calculate(2.5f, 2);/// Valid
}

Output 
int,double
int,double
double,int
double,int  

Overriding Vs Shadowing

Overriding

Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.

Example

  1. public class BaseClass  
  2. {  
  3.     public virtual string GetMethodOwnerName()  
  4.     {  
  5.        return “Base Class”;  
  6.     }  
  7. }  
  8. public class ChildClass : BaseClass  
  9. {  
  10.    public override string GetMethodOwnerName()  
  11.    {  
  12.        return “Child Class”;  
  13.    }  
A method cannot be overriden if:
  • Methods have a different return type
  • Methods have a different access modifier
  • Methods have a different parameter type or order
  • Methods are non virtual or static
Shadowing (method hiding)
A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.

Example
  1. Public class BaseClass  
  2. {  
  3.     public string GetMethodOwnerName()  
  4.     {  
  5.        return "Base Class";  
  6.     }  
  7. }  
  8. public class ChildClass : BaseClass  
  9. {  
  10.     public new string GetMethodOwnerName()  
  11.     {  
  12.        return "ChildClass";  
  13.     }  
Test Code
  1. static void Main(string[] args)  
  2. {  
  3.     ChildClass c = new ChildClass();  
  4.     Console.WriteLine(c.GetMethodOwnerName());  
Output
Output

If we do not use the new keyword the compiler generates the warning:

compiler generate warning

Mixing Method (Overriding and shadowing (Method Hiding))

We can also use shadowing and method overriding together using the virtual and new keywords. This is useful when we want to further override a method of the child (derived) class.

Example
  1. public class BaseClass  
  2. {  
  3.     public virtual string GetMethodOwnerName()  
  4.     {  
  5.         return "Base Class";  
  6.     }  
  7. }  
  8. public class ChildClass : BaseClass  
  9. {  
  10.     public new virtual string GetMethodOwnerName()  
  11.     {  
  12.         return "ChildClass";  
  13.     }  
  14. }  
  15. public class SecondChild : ChildClass  
  16. {  
  17.     public override virtual string GetMethodOwnerName()  
  18.     {  
  19.         return "Second level Child";  
  20.     }  
Shadowing Vs Overriding
Shadowing Overriding
Shadowing is a VB.Net concept. It also known as method hiding in C#. Using this concept we can provide a new implementation for the base class method without overriding it. Overriding allows us to re-write a base class function with a different definition.
Using the “new” keyword we can do the shadowing or method hiding. C# uses the virtual/abstract and override keyword for method overriding.
Shadowing redefines an entire method or function. Overriding redefines only the implementation of a method or function.
Showing is used to protect against subsequent base class modification. Overriding does polymorphism by defining a different implementation.
We can change the access modifier. We cannot change the access modifier. The access modifier must be the same as in the base class method or function.
There is no control of a base class on shadowing. In other words, a base class element cannot enforce or stop shadowing. The base class has some control over the overriding. Using the keyword abstract, the base class forces the child (derived) class to implement the function or method.
Shadowing an element (function method or property) can be inherited further in a child (derived) class. The shadowed element is still hidden. The same as shadowing, overriding an element is inherited further in a derived class and the overridden element is still overridden.
In shadowing, the signature of an element could be different. In overriding, the signature of the element must be the same.
In shadowing, the base class cannot access the newly created child (derived) class method. This is because the base class has the same name of the element.
In concept, the base class can be accessed using the child object's overridden method.
Shadowing  Example
  1. static void Main(string[] args)  
  2. {  
  3.     BaseClass c = new ChildClass();  
  4.     Console.WriteLine(c.GetMethodOwnerName());  
Output

Shadow

In overriding, the base class can be accessed using the child object's overridden method.

Overriding Example
  1. static void Main(string[] args)  
  2. {  
  3.     BaseClass c = new ChildClass();  
  4.     Console.WriteLine(c.GetMethodOwnerName());  
Output

Overriding

We cannot use the new and override keywords together. If you do then the compiler throws a compilation error.
Output

compilation error

http://www.c-sharpcorner.com/UploadFile/ff2f08/overriding-vs-shadowing-in-C-Sharp/
http://msdn.microsoft.com/en-us/library/ms172785.aspx