Tuesday, 1 April 2014

Delegate in c#



Delegate:-



A delegate is an object that can refer to a method. Therefore, when you create a delegate, you are creating an object that can hold a reference to a method.



Furthermore, the method can be called through this reference. In other words, a delegate can invoke the method to which it refers.



public delegate int DelegatSample(int a,int b);



What is the use of Delegates?



Suppose if you have multiple methods with same signature (return type & number of parameters) and want to call all the methods with single object then we can go for delegates.



Delegates are two types



      -   Single Cast Delegates

      -  Multi Cast Delegates



Single Cast Delegates



Single cast delegate means which hold address of single method like as explained in above example.



Multicast Delegates



Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=



Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list



Complete Example of Single cast Delegates

public delegate int DelegatSample(int a,int b);
public class Sampleclass
{
public int Add(int x, int y)
{
return x + y;
}
public int Sub(int x, int y)
{
return x - y;
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();

DelegatSample delgate1 = sc.Add;
int i = delgate1(10, 20);
Console.WriteLine(i);
DelegatSample delgate2 = sc.Sub;
int j = delgate2(20, 10);
Console.WriteLine(j);
}
}

Complete Example of Multicast Delegates

public delegate void MultiDelegate(int a,int b);
public class Sampleclass
{
public static void Add(int x, int y)
{
Console.WriteLine("Addition Value: "+(x + y));
}
public static void Sub(int x, int y)
{
Console.WriteLine("Subtraction Value: " + (x - y));
}
public static void Mul(int x, int y)
{
Console.WriteLine("Multiply Value: " + (x * y));
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();
MultiDelegate del = Sampleclass.Add;
del += Sampleclass.Sub;
del += Sampleclass.Mul;
del(10, 5);
Console.ReadLine();
}
}

6 important C# Delegate interview questions and answers

What is a delegate?

Delegate is a safe pointer to a method / function. So rather than pointing to the actual function you point to a delegate and via this delegate you can invoke the methods / functions.

What is a point of a pointer when we can invoke the actual function/method?

Many times rather than pointing to actual methods , we would like to point to abstraction. For example rather than pointing to “Add(int,int)” , “Substract(int,int)” , we create an abstract delegate pointer “Operation(int,int)” and point to any of them above.

What are the uses of delegates ?

There are two basic uses of delegates :-
  • Callbacks / events.
  • Method and function abstraction.
What are events ?

Events are encapsulation over delegates.

What are multi-cast delegates ?

If we want one delegate to point to multiple methods / function we use multicast delegates.

What are the different ways of creating a delegate in C# ?
  • Action
  • Func
  • Predicate
  • Lambda
  • Anonymous  types

Thursday, 13 February 2014

Creating an Asp.Net MVC Application :-

Step-1 Open Visual Studio 2010

Step-2 Click on New Project 

 


Step-3 Select Web from Installed Template[Left Side] 

Select ASP.NET MVC 4 web Application

Select Name & Location & Press OK

Step-4 Select Internet Application from Project Template

Select View Engine as Razor

Step-5 On Solution Explorer, See the structure of folder


Step-6 This is solution structure

Step-7 This is output


Versions of MVC


 Below is a detailed table of differences. But during an interview it’s difficult to talk about all of them due to time limitation. So I have highlighted the important differences that you can run through before the interviewer.

MVC 2  MVC 3 MVC 4 
  • Client-side validation
  • Templated Helpers Areas
  • Asynchronous Controllers
  • Html.ValidationSummary Helper Method
  • DefaultValueAttribute in Action-Method
  • Parameters binding
  • Binary data with Model Binders
  • DataAnnotations Attributes
  • Model-Validator Providers
  • New RequireHttpsAttribute Action Filter
  • Templated Helpers
  • Display Model-Level Errors
  • Razor
  • Readymade project templates
  • HTML 5 enabled templates
  • Support for Multiple View Engines, JavaScript, and AJAX
  • Model Validation Improvements
  • ASP.NET Web API
  • Refreshed and modernized default project templates. New mobile project template.
  • Many new features to support mobile apps
  • Enhanced support for asynchronous methods

Intro to ASP.NET MVC 4

Introduction     
ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. 
In this article I will give an introduction to ASP.NET MVC 4 from a beginner’s perspective.

What is ASP.NET MVC ?  

ASP.NET supports three different development models named Web Pages, MVC and Web Forms.

ASP.NET MVC is a web application development framework built on top of Microsoft’s .NET Framework. ASP.NET Web Form was a replacement for Microsoft’s Active Server Pages (ASP) but ASP.NET MVC is not a replacement for ASP.NET Web Forms and it’s just an alternate way of making an ASP.NET website.

ASP.NET MVC is open source!- In March 2012, Scott Guthrie announced on his blog that Microsoft had released part of their web stack (including ASP.NET MVC, Razor and Web API) under an open source license (Apache License 2.0).

Why MVC ?  

ASP.NET MVC helps to reduce the complexity of the web application by dividing an application into three layers, Model, View and Controller. This separation (loose coupling) helps in some long term benefits like isolation of components while development and also this separation provides better support for test-driven development (TDD). 
ASP.NET MVC web site are good in performance and also easy to maintain. 

Architecture of MVC






MVC Stands for Model, View & Controller. It is a design pattern to make application in parallel programming.

The Model is the part of the application that handles the logic for the application data.
Often model objects retrieve data (and store data) from a database.

The View is the parts of the application that handles the display of the data.
Most often the views are created from the model data.

The Controller is the part of the application that handles user interaction.
Typically controllers read data from a view, control user input, and send input data to the model.

The MVC separation helps you manage complex applications, because you can focus on one aspect a time. For example, you can focus on the view without depending on the business logic. It also makes it easier to test an application.

The MVC separation also simplifies group development. Different developers can work on the view, the controller logic, and the business logic in parallel.

Friday, 24 January 2014

Topics to be discussed in MVC 4






  1. An Introduction to ASP.NET MVC 4
  2. Versions
  3. Creating An ASP.Net MVC Application
  4. Features
  5. Controllers
  6. Views
  7. Models
  8. Html Helpers
  9. Data Annotations and Validations
  10. CRUD Operations On Database Using ASP.Net MVC
  11. Filters
  12. URL Routing
  13. Master Pages and Layout Pages
  14. Caching In ASP.Net MVC
  15. Authentication In ASP.Net MVC
  16. Working With Ajax In JQuery
  17. Security
  18. Internationalization
  19. Dependency Injection
  20. ASP.Net MVC 4 Features
  21. Testing
  22. Deployment
  23. NuGet (Packages)
  24. Introduction to ASP.NET MVC 5

Welcome to my blog....