Monday, 3 March 2014
Monday, 24 February 2014
Difference between function and method
The programming langauages have two concepts functions and methods. functions are defined in structural language and methods are defined in object oriented langauge. The difference between both is given below :
Functions
- Functions have independent existence means they can be defined outside of the class. Ex:- main() function in C, C++ Language
- Functions are defined in structured languages like Pascal,C and object based language like javaScript
- Functions are called independently.
- Functions are self describing unit of code.
//function main in C void main() { int a,b,c; a=5; b=6; c=a+b; printf("Sum is : %d",c); }
Methods
- Methods do not have independent existence they are always defined with in class. Ex:- main() method in C# Language that is defined with in a class
- Methods are defined in object oriented languages like C#, Java
- Methods are called using instance or object.
- Methods are used to manipuate instance variable of a class.
//method sum in C# class demo { int a,b,c; public void sum() { a=5; b=6; c=a+b; Console.WriteLine("Sum is : {0}",c); } }
Difference Between Constant and ReadOnly and Static
Constant and ReadOnly keyword are used to make a field constant which value cannot be modified. Static keyword is used to make members static that can be shared by all the class objects. In this article, I am going to explain the difference among these three.
Constant
- Constant fields or local variables must be assigned a value at the time of declaration and after that they cannot be modified. By default constant are static, hence you cannot define a constant type as static.
- A const field is a compile-time constant. A constant field or local variable can be initialized with a constant expression which must be fully evaluated at compile time.
void Calculate(int Z) { const int X = 10, X1 = 50; const int Y = X + X1; //no error, since its evaluated a compile time const int Y1 = X + Z; //gives error, since its evaluated at run time }
- Constants can be marked as public, private, protected, internal, or protected internal access modifiers.
- Use the const modifier when you sure that the value a field or local variable would not be changed.
ReadOnly
- A readonly field can be initialized either at the time of declaration or with in the constructor of same class. Therefore, readonly fields can be used for run-time constants.
class MyClass { readonly int X = 10; // initialized at the time of declaration readonly int X1; public MyClass(int x1) { X1 = x1; // initialized at run time } }
- Explicitly, you can specify a readonly field as static since, like constant by default it is not static. Readonly keyword can be apply to value type and reference type (which initialized by using the new keyword) both. Also, delegate and event could not be readonly.
- Use the readonly modifier when you want to make a field constant at run time.
- The static keyword is used to specify a static member, which means static members are common to all the objects and they do not tied to a specific object.
- This keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
class MyClass { static int X = 10; int Y = 20; public static void Show() { Console.WriteLine(X); Console.WriteLine(Y); //error, since you can access only static members } }
- If the static keyword is applied to a class, all the members of the class must be static.
- Static methods can only access static members of same class. Static properties are used to get or set the value of static fields of a class.
- Static constructor can't be parameterized and public. Static constructor is always a private default constructor which is used to initialize static fields of the class.
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.
- 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
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
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
Subscribe to:
Posts (Atom)