|
C# FAQ
    
1. What is value type and reference type in C#?
* When a value-type object is created, C# allocates a single space in memory, and puts the contents of the object into it. Primitive types such as int, float, bool or char are also value types, and they are instantiated in the same way. When the runtime deals with a value type, it's dealing directly with its underlying data and this can be very efficient, particularly with primitive types.
* Variables of type reference do not hold the actual data for the object (like value types); instead they hold a reference to the data. The data is allocated on the heap and is automatically garbage collected when it is no longer in use.
* Value types are stored in stack and reference types are stored in heap.
2. What is boxing and unboxing?
Boxing and unboxing allow us to convert value types to reference types and vice versa.
Boxing is the term sed to describe the transformation of a value type to a refernce type. Basically, the
runtime creates a temporary refernce-type "box" for the object on the heap.
int i = 50;
object o = i;
|
Unboxing is the term used to describe the reverse process, where the value of a reference type is cast to a value type.
int i = 50;
object o = i;
int j = (int) o;
|
We can obly unbox a variable that has previously been boxed.
3. What is a delegate?
* A delegate is a way to define a reference to a method that should be used for a callback. Delegates define the format of the method that should be used to handle an event. A method of that delegates type may be called when a particular event is raised.
4. Explain the differences in an interface and an abstract class, and reasons to use one or the other.
* Interface is a contract between a consumer and a provider, and should remain fixed throughout the life of the application because changes to an interface require changes to both consumer and provider code. An interface only contains methods; no fields, and cannot be instantiated.
* An abstract class should contain basic functionality that may be inherited or overridden by derived / child classes. An abstract class may have methods and fields, but some of the methods may be abstract (must be implemented by derived classes) while others are virtual and provide a default implementation that may be overridden in derived classes. Abstract classes may house common functionality that is shared in all derived classes.
5. Describe the methods for ensuring that code is threading safe.
* Using the lock keyword, the .NET runtime will block additional threads from accessing the code within the lock while another thread is running inside
it.
The general form of lcok is shown here:
lock(object){
// statements to be synchronized
}
|
6. What are the capabilities and limitations of the .NET Thread pool?
* Advantage: Threads are created on demand and left in the threadpool for later use so there is no overhead of creating a new thread on each request.
* Disadvantage: The threadpool is limited to 25 threads per processor per process in .NET 2.0
7. How can data be passed between multiple threads in an application?
* Either when creating the thread by passing state information in with the ThreadStart
or by subscribing to event notifications on different threads and firing events to send data to the other threads.
8. What is the process for implementing a web service in C#?
* Create a web service project (asmx), then add a method and put the [WebMethod] attribute on the line before the method declaration.
9. What is the process for consuming a web service in C#?
* Create a web service proxy either using Visual Studio to create a web reference or by using the command line WSDL tool, then call methods on the proxy and the proxy will create the SOAP envelope and send them to the server.
10. What is the purpose of WSDL and XSD in web services?
* The WSDL describes the functionality and data types that a web service provides.
* The XSD describes the complex data types that may be used in a web service. A web service may refer to one or more XSD files for defining its complex data types.
11. What is the class that all remote applications inherit from?
* Either MarshalByReferenceObject or MarshalByValueObject
12. What attribute must be marked on any data type (class) that should be passed over remote channels?
* [Serializable]
13. What channels are available as part of the .NET Framework for remote communications?
* TCP / binary and HTTP / SOAP
|