Wednesday, August 27, 2008

Serialization - XML serialization

Q. What is Serialization

Ans : Serialization can be defined as the process of storing the state of an object to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.

When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized. The following sections examine the robust serialization mechanism provided with the .NET Framework and highlight a number of important features that allow you to customize the process to meet your needs.

Serialization in .NET allows the programmer to take an instance of an object and convert it into a format that is easily transmittable over the network, or even stored in a database or file system. This object will actually be an instance of a custom type, including any properties or fields you may have set.

XML serialization converts (serializes) the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport.
Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform. For example, XML Web services created using ASP.NET use the XmlSerializer class to create XML streams that pass data between XML Web service applications throughout the Internet or on intranets. Conversely, deserialization takes such an XML stream and reconstructs the object.
XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML.
To serialize or deserialize objects, use the XmlSerializer class. To create the classes to be serialized, use the XML Schema Definition tool.

Monday, June 2, 2008

.Net Framework

Q1. What is .Net Framework

Ans - . NET is a software platform. It's a language-neutral environment for developing .NET applications that can easily and securely operate within it.
The .NET Framework has two main components: the Common Language Runtime (CLR) and the .NET Framework class library.
The .net framework allows objects, classes and functions created in multiple programming languages the ability to communicate effectively amongst themselves. During code compilation whithin the .Net framework, source code is converted into MSIL(Microsoft Intermediate Language). MSIL, or IL, is the common language created that the CLR(Common Language Runtime) can read and understand. Once .Net code is compiled into intermediate language(MSIL, IL) objects written in VB.Net may reference or inherit functionality from objects written in Visual C#.Net, managed C# or any other .Net language.
For each source code compiler (VB.Net, C#.Net, etc), there is a minimum set of coding standards that must be met. The minimum set of coding standards that must be met to compile .Net code into MSIL code is known as CLS-Common Language Specification. The role of the CLS is to ensure that all generated code (MSIL) that meets the minimum set of coding standards can operate successfully whithin the .Net framework.
The CTS (Common Type System) handles conversion of programming language data types into .Net compatible(MSIL) data types. The implicit benefit of the CTS is the reduction of development time when attempting to coordinate data types between two sets of different programming language code. The languages that are currently available in the VS.Net framework include: VB, VC#, VC++ & JScript Scripting Language.
The class library is a comprehensive collection of reusable types that you can use to develop traditional command-line, WinForm (graphical user interface) applications, Web Forms and XML Web services.

Q2. What is a CLR?

Ans - Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework. All Languages have runtime and its the responsibility of the runtime to take care of the code execution of the program. For example VB6 has MSVBVM60.DLL, Java has Java Virtual Machine etc. Similarly .NET has CLR. Following are the responsibilities of CLR

  • Garbage Collection :- CLR automatically manages memory thus eliminating memory leaks. When objects are not referred GC automatically releases those memories thus providing efficient memory management.
  • Code Access Security :- CAS grants rights to program depending on the security configuration of the machine. Example the program has rights to edit or create a new file but the security configuration of machine does not allow the program to delete a file. CAS will take care that the code runs under the environment of machines security configuration.
  • Code Verification :- This ensures proper code execution and type safety while the code runs. It prevents the source code to perform illegal operation such as accessing invalid memory locations etc.
  • IL( Intermediate language )-to-native translators and optimizer’s :- CLR uses JIT and compiles the IL code to machine code and then executes. CLR also determines depending on platform what is optimized way of running the IL code.

Q3. What is a CTS?

Ans - As described above in the Q1, In order that two language communicate smoothly CLR has CTS (Common Type System). Example in VB you have "Integer" and in C++ you have "long" these datatypes are not compatible so the interfacing between them is very complicated. In order to able that two different languages can communicate Microsoft introduced Common Type System. So "Integer" datatype in VB6 and "int" datatype in C++ will convert it to System.int32 which is datatype of CTS. CLS which is covered in the coming question is subset of CTS.

Q4. What is CLS(Common Language Specification)

Ans - This is a subset of the CTS which all .NET languages are expected to support. CLS is nothing but guidelines that languages have to follow so that it can communicate with other .NET languages in a seamless manner.

Wednesday, May 14, 2008

ASP.Net general questions

Q1. What’ is the sequence in which ASP.NET events are processed ?
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

Q 1. What is a delegate ?
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_ : this is a public method that calls the static Combine method of System.Delegate in order to add another method to its internal invocation list. This method is however not used explicitly. The same effect is achieved by using the += operator as specified before.
remove_ : this is also a public method that calls the static Remove method of System.Delegate in order to remove a receiver from the event's invocation list. This method is also not called directly. Its job is done by the "-=" operator.
raise_ : a protected method that calls the compiler generated Invoke method of the delegate, in order to call each method in the invocation list.

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

OOPS Concepts are described below: -
  • 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.