Monday, 24 February 2014

Interface C#


Interface  

An interface acts as a contract between itself and any class or struct which implements it. It means a class that implement an interface is bound to implement all its members.
Interface has only member’s declaration or signature and implicitly every member of an interface is public and abstract.
For example: The most common use of interfaces is, within SOA (Service Oriented Architecture). In SOA (WCF), a service is exposed through interfaces to different clients. Typically, an interface is exposed to a group of clients which needs to use common functionalities.

interface IStore
{
 void Read();
 void Write();
}
 
interface ICompress
{
 void Compress();
 void Decompress();
}
 
public class Document : IStore, ICompress
{
 #region IStore
 
 public void Read()
 {
 Console.WriteLine("Executing Document's Read Method for IStore");
 }
 
 public void Write()
 {
 Console.WriteLine("Executing Document's Write Method for IStore");
 }
 
 #endregion // IStore
 
 #region ICompress
 
 public void Compress()
 {
 Console.WriteLine("Executing Document's Compress Method for ICompress");
 }
 public void Decompress()
 {
 Console.WriteLine("Executing Document's Decompress Method for ICompress");
 }
 #endregion // ICompress
} 

Interface Features:
  • An interface doesn't provide inheritance like a class or abstract class but it only declare members which an implementing class need to be implement.
  • It cannot be instantiated but it can be referenced by the class object which implements it. Also, Interface reference works just like object reference and behave like as object.
IStore IObjStore = new Document();
ICompress IObjCompress = new Document();
  • It contains only properties, indexers, methods, delegates and events signature.
  • It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.
  • Members of an interface cannot have any access modifiers even public.
  • Implicitly, every member of an interface is public and abstract. Also, you are not allowed to specify the members of an interface public and abstract or virtual.
  • An interface can be inherited from one or more interfaces and can extend another interface.
  • A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.
  • Implementing multiple interfaces by a class, sometimes result in a conflict between member signatures. You can resolve such conflicts by explicitly implementing an interface member.
  • It is a good practice to start all interface names with a capital “I” letter.
Where to use Interfaces?

  • Need to provide common functionality to unrelated classes.
  • Need to group objects based on common behaviors.
  • Need to introduce polymorphic behavior to classes since a class can implements more than one interfaces.
  • Need to provide more abstract view to a model which is unchangeable.
  • Need to create loosely coupled components, easily maintainable and pluggable components (like log4net framework for logging) because implementation of an interface is separated from itself.
Disadvantage of Interface:
  • The main issue with an interface is that when you add a new members to its, then you must implement those members within all of the classes which implement that interface.
  • Interfaces are slow as these required extra in-direction to find corresponding method in in the actual class.

Abstract Class C#


Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes.
Abstract class members marked as abstract must be implemented by derived classes.
The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.

For example: A class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class.


abstract class ShapesClass
{
 abstract public int Area();
}
class Square : ShapesClass
{
 int side = 0;
 
 public Square(int n)
 {
 side = n;
 }
 // Override Area method 
 public override int Area()
 {
 return side * side;
 }
}
 
class Rectangle : ShapesClass
{
 int length = 0, width=0;
 
 public Rectangle (int length, int width)
 {
 this.length = length;
 this.width = width;
 }
 // Override Area method 
 public override int Area()
 {
 return length * width;
 }
}
Abstract Class Features:
  • An abstract class cannot be instantiated.
  • An abstract class contain abstract members as well as non-abstract members.
  • An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
  • A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
  • An abstract class can be inherited from a class and one or more interfaces.
  • An Abstract class can has access modifiers like private, protected,internal with class members.But abstract members cannot have private access modifier.
  • An Abstract class can has instance variables (like constants and fields).
  • An abstract class can has constructors and destructor.
  • An abstract method is implicitly a virtual method.
  • Abstract properties behave like abstract methods.
  • An abstract class cannot be inherited by structures.
  • An abstract class cannot support multiple inheritance.
Usage:
  • Need to create multiple versions of your component since version is not a problem with abstract class. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.
  • Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

Boxing and Unboxing


Boxing
Implicit conversion of a value type (int, char etc.) to a reference type (object), is known as Boxing. In boxing process, a value type is being allocated on the heap rather than the stack.

Unboxing
Explicit conversion of same reference type (which is being created by boxing process); back to a value type is known as unboxing. In unboxing process, boxed value type is unboxed from the heap and assigned to a value type which is being allocated on the stack.

Real Life Example
int i = 10;
ArrayList arrlst = new ArrayList();

//ArrayList contains object type value

//So, int i is being created on heap

arrlst.Add(i); // Boxing occurs automatically

int j = (int)arrlst[0]; // Unboxing occurs

Tuesday, 4 February 2014

Hosting the MVC application using the IIS

IIS, to run the application instead of the built-in asp.net development server.




1. In the solution explorer, right click on the project and select "Properties"
2. Click on "Web" tab
3. Select "Use Local IIS Web Server" radio button
4. Notice that the Project Url is set to http://localhost/MVCDemo by default
5. Finally click on "Create Virtual Directory" button

Creating the first MVC application

Follow the steps as shown and create your hello world mvc application

1.
 Open visual studio
2. Click File > New Project
3. Select "Web" from "Installed Templates" section
4. Select ASP.NET MVC 4 Web Application(If dont have  MVC 4 means then ASP.NET MVC 3) 
5. Set Name="MVCDemo"
6. Click OK
7. Select "Empty" template. Select "Razor" as the ViewEngine. There are 2 built in view engines - Razor and ASPX. Razor is preferred by most mvc developers
8. At this point you should have an mvc application created.

Notice that in the solution explorer, you have several folders - Models, Views, Controllers etc. As the names
 suggest these folders are going to contain Models, Views, and Controllers. 

·         Model , which represents the underlying, logical structure of data in a software application and the high-level class associated with it. This object model does not contain any information about the user interface.
·         View , which is a collection of classes representing the elements in the user interface (all of the things the user can see and respond to on the screen, such as buttons, display boxes, and so forth)
·         Controller , which represents the classes connecting the model and the view, and is used to communicate between classes in the model and view.
Now let's add a controller to our project
1. Right Click on "Controllers" folder
2. Select Add > Controller
3. Set Controller Name = HomeController
4. Leave rest of the defaults and click "Add"
We should have HomeController.cs added to "Controllers" folder. 

At this point run the application by pressing CTRL+F5. Notice that you get an error as shown below.

The First type is as follows
 Right click on the Index() function and Click on "Add View"
Notice that, the view name in "Add View" dialog box matches the name of the controller action method.Select "Razor" as the view engine.Leave the rest of the defaults as is, and click "Add" button.

The Second type is as follows
The following is the function that is automatically added to HomeController class.

public ActionResult Index()
{
    return View();
}

Change the return type of Index() function from "ActionResult" to "string", and return string "Hello from MVC Application" instead of View().

public string Index()
{
    return "Hello from MVC Application";
}

Run the application and notice that, the string is rendered on the screen. When you run the application, by default it is using built-in asp.net development server. In the URL "Home" is the name of the controller and "Index" is the method within HomeController class.
 Finally your first hello world MVC application has been completed

Tuesday, 17 December 2013

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack


Solved : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

Reason:
     The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally.
Solution: 
For Response.Redirect
Response.Redirect ("nextpageURL.aspx", false);
For Server.Transfer
Use Server.Execute instead of Server.Transfer

Separate characters in a string using SPLIT and ARRAY

The SPLIT will separate the characters using a unique character or symbol like + , . @_ - etc. and store values in the ARRAY.

Example :

protected void buttonname_Click(object sender, EventArgs e)
{
string a = It-is-a-demo;

string[] b = a.split('-');  //It will split the characters after each of this special character underscore -

string split1 = b[0];  //It will be stored using the array
string split2 = b[1];  // is will be stored
string split3 = b[2];  // a will be stored
string split4 = b[3];  // demo will be stored

lbllaststring.Text = split4;  //demo will be displayed in the label
}
back to top