Sunday, 17 September 2017

A sample code using delegates C#

   public class Employee
   {
       public int Id;
       public string Name;
       public float Salary;

       public delegate void EmployeeAdd(int id, string name, float sal);

       public EmployeeAdd EmployeeAdded;

       public void AddEmployee(int id, string name, float sal)
       {
           AddToDataBase();
           EmployeeAdded(id,name,sal);  
       }
 }

class Program
{
       static void Main(string[] args)
       {
           Employee objE = new Employee();
           objE.EmployeeAdded = OnEmployeeAdded;
           objE.AddEmployee(1, "Kiran", 500000);
           objE.AddEmployee(2, "Ravi", 750000);

           Console.ReadLine();  
       }

      public static void OnEmployeeAdded(int id, string name, float sal)
      {
       Console.WriteLine("Id : {0}, Name : \"{1}\", Salary :INR {2}.",  id, name, sal);
      }
}


Wednesday, 27 February 2013

Code to Encode, Decode URL and Query Strings in c# and javascript


In C#

using System; // Namespace
System.Uri -- Class Name

Uri.EscapeUriString or HttpUtility.UrlPathEncode --> to Encode actual Url path.
HttpUtility.UrlEncode("Stack Overflow") --> "Stack+Overflow" to encode query string parameters.

System.Uri.UnescapeDataString --> to Decode actual Url path or a string.
HttpUtility.UrlEncode("Stack+Overflow") --> "Stack Overflow" to encode query string parameters.


In JavaScript 

encodeURIComponent --> to encode URI or QueryString data
example:
encodeURIComponent(uri);

decodeURIComponent --> to decode a string that was URI encoded with the encodeURIComponent
example:
decodeURIComponent(uri);

Saturday, 12 May 2012

static / Shared modifier in C# / VB.Net


static in C# or Shared in VB.Net

static - C# ( case sensitive ) / Shared in VB.Net is a modifier, with class scope. Member declared with this modifier cannot be accessed by the object of the class. This modifier is used with other Access Modifiers like private, public, internal, protected, and protected internal.

static / Shared variable gets the scope of application, not the current session of a specific user.

When we want to maintain application level data, we will use variables declared with this modifier.
Example: When we want to maintain the no of active users on our website, we will store the value in a static variable. Whenever a new user logs into the application we will increase the value or decrease it when one leaves.

We should not use this to maintain the data of a specific user.
Example: if we store the current user name in static variable at the time of login, and then it will always give the most recent logged in user name as current username.

Sunday, 12 February 2012

c#, asp.net, mvc, Entity framework interview questions

.net framework related questions

What are generics ?
What is Shadowing ? Tell me whether it is Overloading or Overriding ?
What are Extension methods ?
What are Anonymous methods ?
What are generic templates ?
What are new features in .Net 3.5
What are new features in .Net 4.0

MVC related Questions

Tell me about Model Binding in MVC
Tell me about Action filters in MVC
How do you configure routing in MVC
How MVC request is handled / MVC request life cycle
Tell me about validation in MVC ? Both client side and server side validation
How to create a grid in MVC 3.0 ?
What are new features in MVC 2.0 and MVC 3.0 ?

LINQ

Difference between Where and Any in LINQ

ENtity Framework

How do you call a stored procedure from EF
Tell me about POCO in EF 4.0
What is approach followed in EF ? Whether it is Code first or Design (Schema) first ?

Design related

Tell me about Dependency Injection

C# Asp.net common questions

Q: What is the object used to create Transaction in ADO.NET.  
Ans: Connection is used to create Transaction Object

Q: How data represented in XMLdatadocument.
Ans: in the forms of node and children

Q: What object is used to encapsulate datasource
Ans: Dataadapter encapsulates datasource.

Q: Namespace required for dataset to work?
Ans: System.data
   
Q: How will you make sure that dataset is not empty?
ANS: Before doing any operation Check isNothig (Dataset), this will tell whether dataset is empty or not

Q: What is ASMX?
ANS: web service files extension

Q: What is ASHX?
ANS: ASHX files are HTTP handlers written using .NET. Basically you can think of them as .NET ISAPI Extensions, except that they are much, MUCH easier to write.

Q: Where can you maintain connection string in specific ASP.NET application?
Ans: In the Application Level Web.Config file
Machine.config - This will come up with .NET Frame work, this is system level config file
Web.config - This will be created once u create web application, this is will contain application level configs
App.config - This will be created once u create windows application, this is will contain application level configs

Q:What are possible entry points for an Assembly.
ANS: Main()

Q: How do Asp.Net maintain page level control state?
Ans:-View state

Q:What are different ways to maintain session state in Asp.Net
ANS: In Proc, SQL Server and State Server. We shouldn’t say OutProc.
We can say like – In Process and Out Process.
Under Out Process… It can be either Sql Server or State Server.

Q: How do you use cookieless session in Asp?


Q: What is the entry point for c#?
ANS: Main – (M is Capital)  

Q: XSL?    
ANS: eXtensible Markup Language Stylesheet Language

Q: XML preserves white spaces?
ANS: Yes

Q: XML elements can't be empty?
ANS: Yes, [Verify It]

Q: What are different Access Modifiers in C#?
ANS: Private, public, protected, Internal, ProtectedInternal

Q: What method below can be overridden
Abstractmethod()
Virtual method()
Void method()
Override method()
Int method()

Ans:-Virtual method()
Suggestion: -- whether there are any Virtual methods in the given choices. I think INIT is a virtual method. (if Init Method is Misspelled as Int method) . Check it once.

Q: How to find methods of an Assembly?
1) Typelibviewer        2) ILDASM    3) Reflection API       4) Registry

ANS: Reflection API

Q: How to check datareader is closed or not?
Ans: IsClosed

Q: What are XSLT processors.


Q: What is the base class of .Net
Ans: System.Object (Be careful Naming Convention Follows Pascal Case)

Q: XML is case sensitive?
Ans: Yes

Q: How to represent XSL?

Q: What is the command, to restrict part of XML file?

Q: How can we convert an assembly into Shared assembly?
ANS:  By drag and drop the assembly files into c:/WINNT/assembly/
By Providing Strong name and place it into a GAC.
By Providing Strong name and place it into a GAC.

Q: What are different types of assemblies available in Asp.Net?
Ans: Public, Private and Shared (Shared contain culture specific info)

Q: What are variables available in Asp.Net?


Q: What is the provider is used for MS ACCESS?
Ans: Microsoft.Jet.OleDB

Q: What is the namespace is used to connect MS ACCESS?
Ans: System.Data, System.Data.OleDB.

Q: DTD stands for?
Ans: Document Type Definition

Q: App domain()?
Ans:

Q: What is the differences between Datagrid, datlist, datareader?
Ans: DataReader used to retrieve data from Database, remaining are used to display the data.

Q: Which namespace is used to interact with Local hard drive files?
Ans: System.IO

Q: What is Commandbuilder?
Ans: An object that creates Select, Update & Delete commands based on the Select Command provided to it.

Q: Machine.config? web.config?
Ans: Machine.config is the configuration file. That applies to the system.
We can override the configuration with Web.config.
Even we can override the web.config, with a new web.config in the specific folder.

Q: What is Global.asa?
Ans: ASA – is application storage area. (Used in ASP). In .Net it is ASAX.

Q: How GUI+BL+DB connect each other?
Ans: through messaging. One of the basic features of O.O.P.S

Q: Call by value? Call by ref? call by out?
Ans: call by value, we pass copy of the parameter.
Call by ref, we pass ref of the INITIALISED object.
Call by out, we pass ref of the object (need not be initialized)

Q: What is the diff between System.Array and System.Arraybuilder?

Q: Can I override elements which are inherited from Base class?
ANS: Yes

Q: Can I restrict the methods/fields with in the class members?

Q: Static Constructors?
ANS: A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

Q: How can we import "COM" components into .Net application?
Ans: By creating and Using com callable wrapper class (ccw) for the COM component

Q: How can we sort elements in an array as descending order?

Q: What is the difference between Debug Class and Trace Class?
ANS: Documentation looks the same.  Use Debug class for debug builds, use Trace class for both debug and release builds.

Q: Is all access modifiers are same for both get/post methods?
ANS: NO

Q: Delegate?

Q: What is the diff between system.string.copyto()  and System.string.clone().
ANS: The first one performs a deep copy of the array, the second one is shallow.  A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to.  The references in the new Array point to the same objects that the references in the original Array point to.  In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

Simple / single-cast Delegate explained

Delegate is type that references a method of a given signature [that is for given input and output
parameters]. Namespace is System, referred as “System.Delegate”. Delegates are type safe as you can
refer a method with the signature specified in the declaration of the delegate.

For example:

public double SquareOf(int val)
{
return val * val;
}

public delegate double MyDelegate(int param);
MyDelegate myDel = new MyDelegate(SquareOf);

In the above code we have declared a delegate MyDelegate with input parameter of type int and output
of type double. Using this we can refer any method with input parameter of type int and output type
double.

public delegate double MyDelegate(int param);

We have instantiated the delegate as follows.

MyDelegate myDel = new MyDelegate(SquareOf);

How we will use delegate

namespace ConsoleApp1
{
class Program
{
public static double SquareOf(int val)
{
return val * val;
}

public delegate double MyDelegate(int param); <= Declaration
static void Main(string[] args)
{
MyDelegate myDel = new MyDelegate(SquareOf); <= Instantiation
double res = myDel(15); <= Using of delegate
Console.WriteLine("Square of 15 is :" + res.ToString());
Console.ReadLine();
}

}

}

Output will be: Square of 15 is :225

types of delegates

Delegate is type that references a method of a given signature [that is for given input and output
parameters]. Delegates are type safe as you can refer any method with the signature specified in the
declaration of the delegate.

Delegates are of two types

1 Simple / Singlecast delegate.

2. Multicast delegate.

Simple / Singlecast delegate

Type name is “System.Delegate”. Using this we can reference a method with matching signature of
delegate declaration.

Multicast delegate

Type name is “System.MulticastDelegate”. Using this we can reference any number methods with
matching signature of delegate declaration. Multicast delegate is being used asp.net for event handling
mechanism due to which people call this as “Event delegate”.

For the methods referenced by multicast delegate return type should be void. When you
observe the events in your asp.net webpage code behind, you will notice that all the event methods has
return type void.