Wednesday, May 14, 2008
ASP.Net general questions
Ans - Following is the sequence in which the events occur :-
√ Page_Init.
√ Page_Load.
√ Control events
√ Page_Unload event.
Page_init event only occurs when first time the page is started, but Page_Load occurs in subsequent request of the page.
Q2. In which event are the controls fully loaded ?
Ans - During Page_load event all controls get fully loaded. Controls are also accessed in Page_Init events but you will see that viewstate is not fully loaded during this event.
Q3. How can we identify that the Page is PostBack ?
Ans - Page object has a "IsPostBack" property which can be checked to know that is the page posted back or not.
Q4. Where is ViewState information stored ?
Ans - Viewstate is stored in a HTML Hidden Field on the client side.
Q5. What is "AutoPostBack" feature in ASP.NET ?
Ans - If we want the control to automatically postback in case of any event, we will need to check this attribute as true. Example on a Dropdown change we need to send the event immediately to the server side then set the "AutoPostBack" attribute to true.
Friday, May 2, 2008
Delegates and Events
Ans - Delegate is a class that can hold a reference to a method or a function. A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. Delegate class has a signature and it can only reference those methods whose signature is compliant with the class. The delegate object can be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Delegates are type-safe functions pointers or callbacks.
An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
The signature of a single cast delegate is shown below:
delegate result-type delname ([parameters]);
where:
result-type : The result type, which matches the return type of the function.
delname : The delegate name.
parameters: The Parameters, that the function takes.
A very basic example:
using System;
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
}
}
OUTPUT
called by delegate ...
Q 2. What are events ?
Ans - As compared to delegates events works with source and listener methodology. So listeners who are interested in receiving some events they subscribe to the source. Once this subscription is done the source raises events to its entire listener when needed. One source can have multiple listeners.
Declaring an event is directly tied to a delegate. A delegate object encapsulates a method so that it can be called anonymously. An event is a mechanism by which a client class can pass in delegates to methods that need to be invoked whenever "something happens". When it does, the delegate(s) given to it by its clients are invoked.
To declare an event in C#, use the following syntax:
public delegate void testDelegate(int a);
public event testDelegate MyEvent;
Once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is nothing but a method that is called using a delegate. Use the += operator to associate an event with an instance of a delegate that already exists.
For example:
Myform.MyEvent += new testEvent(MyMethod);
An event handler may also be detached as follows:
MyForm.MyEvent -= new testEvent(MyMethod);
In C#, events may be raised by just calling them by name similar to method invocation, say MyEvent(10).
Whenever an event is defined for a class, the compiler generates three methods that are used to manage the underlying delegate:
add_
remove_
raise_
An event handler in C# is a delegate with a special signature, as
public delegate void MyEventHandler(object sender, MyEventArgs e);
The first parameter (sender) in the above declaration specifies the object that fired the event. The second parameter (e) of the above declaration holds data that can be used in the event handler. The class MyEventArgs is derived from the class EventArgs. EventArgs is the base class of more specialized classes, like MouseEventArgs, ListChangedEventArgs, etc. For GUI event, you can use objects of these specialized EventArgs classes without creating your own specialized EventArgs classes. However, for non GUI event, you need to create your own specialized EventArgs class to hold your data that you want to pass to the delegate object. You create your specialized EventArgs class by deriving from EventArgs class.
public class MyEventArgs : EventArgs{
public string m_myEventArgumentdata;
}
In case of event handler, the delegate object is referenced using the key word event as follows:
public event MyEventHandler MyEvent;
Tuesday, April 29, 2008
Abstract Class & Interface
Q 1. What are abstract classes ?
Ans - Following are the features of a abstract class :-
- You can not create a object of abstract class.
- Abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built.
- Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
- Abstract classes can have implementation or pure abstract methods which should be implemented in the child class.
- In VB.NET abstract classes are created using "MustInherit" keyword.In C# we have "Abstract" keyword.
An abstract class only allows other classes to inherit from it and cannot be instantiated. When we create an abstract class, it should have one or more completed methods but at least one or more uncompleted methods and these must be preceded by the key word abstract. If all the methods of an abstract class are uncompleted then it is the same as an interface, but there is a restriction that it cannot make a class inherit from it, which means it can not work as a base class.
Q 2. What is a Interface ?
Ans - An interface is defined by the key word interface. An interface has no implementation; it only has the definition of the methods without the body. When we create an interface, we are basically creating a set of methods without any implementation. A class implementing an interface must provide the implementation of the interface members. All the methods and properties defined in an interface are by default public and abstract.
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing Idisposable interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
- Single Class can implement multiple interfaces.
- If a class implements a interface then it has to provide implementation to all its methods.
Q 3. What is the difference between abstract classes and interfaces?
Ans - Following are the differences between abstract and interfaces :-
- An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
- An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
- An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
- A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
- Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
- Abstract classes are faster than interfaces.
Sunday, April 27, 2008
OOPS Concepts
- Class - A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It’s a comprehensive data type which represents a blue print of objects. It’s a template of object.
- Object - An object is the basic building block of object-oriented programming
- Instance - One can have an instance of a class or a particular object. The instance is the actual object created at runtime. The set of values of the attributes of a particular object is called its state. The object consists of state and the behaviour that's defined in the object's class.
- Method - In a simple language it is An object's abilities.
- Message Passing - The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing.
- Inheritance - It means reusability. Example we have VEHICLE class and we can inherit this class to make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class. It shows more of a parent-child relationship. This kind of hierarchy is called inheritance.
- Encapsulation - Encapsulation conceals the functional details of a class from objects that send messages to it in other words It is a process of hiding all the internal details of an object from the outside world. It means that all of the object's data is contained and hidden in the object and access to it restricted to members of that class. Programming languages aren't quite so strict and allow differing levels of access to the object's data. The first three levels of access shown below are in both C++ and C#, with the last two in C#.
1 . Public : All Objects can access it.
2 . Protected : Access is limited to members of the same class or descendants.
3 . Private : Access is limited to members of the same class.
4 . Internal : Access is limited to the current assembly.
5 . Protected Internal : Access is limited to the current assembly or types derived from the containing class. - Abstraction - It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in world.It’s a model of real world or concept.
- Polymorphism - Polymorphism allows you to treat derived class members just like their parent class' members.
When inheritance is used to extend a generalized class to a more specialized class, it includes behavior of the top class(Generalized class). The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same. It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.
Saturday, April 26, 2008
OOPS Interview Question part - I
(Q1 ) What is Object Oriented Programming ?
Ans . Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. Programming techniques may include features such as, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s. Many modern programming language now support OOP.
It is a problem solving technique to develop software systems. It is a technique to think real world in terms of objects. Object maps the software model to real world concept. These objects have responsibilities and provide services to application or other objects.
(Q2) What’s a Class ?
Ans . A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It’s a comprehensive data type which represents a blue print of objects. It’s a template of object.
(Q3) What’s an Object ?
Ans. It is a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Objects are members of a class. Attributes and behavior of an object are defined by the class definition.
(Q4) What is the relation between Classes and Objects ?
Ans. They look very much same but are not same. Class is a definition, while object is a instance of the class created. Class is a blue print while objects are actual objects existing in real world. Example we have class CAR which has attributes and methods like Speed, Brakes, Type of Car etc. Class CAR is just a prototype, now we can create real time objects which can be used to provide functionality. Example we can create a Maruti car object with 100 km speed and urgent brakes.