Tuesday, February 7, 2012

.Net FAQ

C# Frequently Asked Questions

1. Default access specifier

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.


2. Protected Internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

3. Advantages of Delegates

They're a great way of encapsulating a piece of code. For instance, when you attach an event handler to the button, that handler is a delegate. The button doesn't need to know what it does, just how to call it at the right time.

Another example is LINQ - filtering, projecting etc all require the same kind of template code; all that changes is the logic to represent the filter, the projection etc. With lambda expressions in C# 3 (which are converted into delegates or expression trees) this makes it really simple:

var namesOfAdults = people.Where(person => person.Age >= 18)
.Select(person => person.Name);

(That can also be represented as a query expression, but let's not stray too far from delegates.)

Another way of thinking of a delegate is as a single-method interface type. For example, the EventHandler delegate type is a bit like:

public interface IEventHandler
void Invoke(object sender, EventArgs e)

But the delegate support in the framework allows delegates to be chained together, invoked asynchronously, used as event handlers etc.


4. Function Overloading in webservices

Not Possible for SOAP, Possible in WCF

  [OperationContract(Name = "DoWork")]
        string DoWork();

       [OperationContract(Name = "DoWorkWithParam")]
        string DoWork(string param);

The function overloading in Web Service is not as straightforward as in class. While trying to overload member function, we make two or more methods with the same name with different parameters. But this will not work in web services and will show runtime error because WSDL is not supported by the same method name.


5.Web services vs WCF


6. How to consume .net web services from java/python. How dataset will be processed.?
At design time, when the Java developer runs WSDL2Java, there isn't enough information in the schema definition to do anything.Hence, In order for Java developers to consume this Web Service, they'll have to drop down and use their equivalent DOM API directly.
The ultimate solution, however, would be to standardize an XML Schema definition for representing database resultsets that could be supported across all toolkits.


7. Abstract Classes vs. Interfaces

8. What is an asax file.

9.MutliCast delegates

10. Sealed Class

The main purpose of a sealed class to take away the inheritance feature from the user so they cannot derive a class from a sealed class. One of the best usage of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace.
The Pens class represent the pens for standard colors. This class has only static members. For example, Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes. The Brushes.Blue represents a brush with blue color.
So when you're designing your application, you may keep in mind that you have sealed classes to seal user's boundaries.




11. Static Classes and Static Class Members

12.What is the difference between const and static readonly?

13.Static Class vs Singleton

What makes you say that either a singleton or a static method isn't thread-safe? Usually both should be implemented to be thread-safe.

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common IME), so you can pass around the singleton as if it were "just another" implementation.


14.Primary Interop Assemblies

15.What is SOAP Prootcol

16.Access Modifiers

17. Indexes in SQL

18. ExecuteQuery-Vs-ExecuteNonQuery

19. ExecuteQuery VS ExecuteReader

20. idictionary

21. Hash table vs. Dictionary

The Dictionary class has the same functionality as the Hashtable class. A Dictionary of a specific type (other than Object) has better performance than a Hashtable for value types because the elements of Hashtable are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.

22. Layered Archtecture and Multitier architecture
The concepts of layer and tier are often used interchangeably. However, one fairly common point of view is that there is indeed a difference, and that a layer is a logical structuring mechanism for the elements that make up the software solution, while a tier is a physical structuring mechanism for the system infrastructure.


23. Should I use a view, a stored procedure, or a user-defined function


1. A function is a subprogram written to perform certain computations
2. A scalar function returns only a single value (or NULL), whereas a table function returns a (relational) table comprising zero or more rows, each row with one or more columns.
3. Functions must return a value (using the RETURN keyword), but for stored procedures this is not compulsory.
4. Stored procedures can use RETURN keyword but without any value being passed.
5. Functions could be used in SELECT statements, provided they don’t do any data manipulation. However, procedures cannot be included in SELECT statements.
6. A function can have only IN parameters, while stored procedures may have OUT or INOUT parameters.
7. A stored procedure can return multiple values using the OUT parameter or return no value at all.

We can call a SQL Function from ADO.NET code. Say we havea function named ABC(). So we call as SELECT ABC() from ADO.Net code. We cannot calla directly as ABC().