जगदीश खोलिया: Few important C# question...

Wednesday, June 6, 2012

Few important C# question...

1.C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? 
Ans.  Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.

2.Can you declare the override method static while the original method is non-static?
 Ans. No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.

3.Can you prevent your class from being inherited and becoming a base class for some other classes? Ans. Yes, that’s what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class Whatever Base Class Name is.

4. When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)? 
Ans. When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.

5. What’s the difference between System.String and System.StringBuilder classes?
Ans. System.String is immutable; System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

6. What is the use of private constructor ?
Ans. When we declare a class constructor as private , we can not do 2 things:-
  • We can not create a object of the class.
  • We can not inherit the class.

Now the next question which the interviewer will ask , whats the use of such kind of class which can not be inherited neither instantiated.
Many times we do not want to create instances of certain classes like utility , common routine classes. Rather than calling as shown below

7.Who is faster hashtable or dictionary ?
Ans.Dictionary is faster than hashtable as dictionary is a generic strong type. Hashtable is slower as it takes object as data type which leads to boxing and unboxing.

8. What is the difference between const and readonly?

public class Const_V_Readonly
  public const int I_CONST_VALUE = 2;
  public readonly int I_RO_VALUE;
  public Const_V_Readonly()
     I_RO_VALUE = 3;

AssemblyB references AssemblyA and uses these values in code. When this is compiled,
  • in the case of the const value, it is like a find-replace, the value 2 is 'baked into' the AssemblyB's IL. This means that tomorrow if I update I_CONST_VALUE to 20 in the future. Assembly B would still have 2 till I recompile it.
  • in the case of the readonly value, it is like a ref to a memory location. The value is not baked into AssemblyB's IL. This means that if the memory location is updated, Assembly B gets the new value without recompilation. So if I_RO_VALUE is updated to 30, you only need to build AssemblyA. All clients do not need to be recompiled.

9.I want to force the data reader to return only schema of the data store rather than data.
Ans. PobjDataReader = pobjCommand.ExecuteReader (CommandBehavior.SchemaOnly)

10. What are the various methods provided by the dataset object to generate XML?

 XML is one of the most important leap between classic ADO and ADO.NET. So this question is normally asked more generally how can we convert any data to XML format. Best answer is convert in to dataset and use the below methods.
• ReadXML
Read’s a XML document in to Dataset.
• GetXML
This is a function, which returns the string containing XML document.
• Writexml
This writes a XML data to disk.

11.What is the difference between “Dataset” and “Data Reader” ?

Ans.Following are the major differences between “Dataset” and “Data Reader”:-
• “Dataset” is a disconnected architecture, while “Data Reader” has live connection while reading data. If we want to cache data and pass to a different tier “Dataset” forms the best choice and it has decent XML support.

• When application needs to access data from more than one table “Dataset” forms the best choice. 

• If we need to move back while reading records, “data reader” does not support this functionality.

• However, one of the biggest drawbacks of Dataset is speed. As “Dataset” carry considerable overhead because of relations, multiple table’s etc speed is slower than “Data Reader”. Always try to use “Data Reader” wherever possible, as it is meant especially for speed performance.

12. What’s difference between “Optimistic” and “Pessimistic” locking ?

In pessimistic locking when user wants to update data it locks the record and till then no one can update data. Other user’s can only view the data when there is pessimistic locking.
In optimistic locking multiple users can open the same record for updating, thus increase maximum concurrency. Record is only locked when updating the record. This is the most preferred way of locking practically. Now a days in browser based application it is very common and having pessimistic locking is not a practical solution.

13.How many ways are there to implement locking in ADO.NET?

Following are the ways to implement locking using ADO.NET:-
• When we call “Update” method of Data Adapter it handles locking internally. If the Dataset values are not matching with current data in Database, it raises concurrency exception error. We can easily trap this error using Try. Catch block and raise appropriate error message to the user.
• Define a Date time stamp field in the table. When actually you are firing the UPDATE SQL statements, compare the current timestamp with one existing in the database. Below is a sample SQL which checks for timestamp before updating and any mismatch in timestamp it will not update the records. This I the best practice used by industries for locking.

Update table1 set field1=@test where Last Timestamp=@Current Timestamp

• Check for original values stored in SQL SERVER and actual changed values. In stored procedure check before updating that the old data is same as the current Example in the below shown SQL before updating field1 we check that is the old field1 value same. If not then some one else has updated and necessary action has to be taken.

Update table1 set field1=@test where field1 = @oldfield1value
Locking can be handled at ADO.NET side or at SQL SERVER side i.e. in stored procedures. For more details of how to implementing locking in SQL SERVER read “What are different locks in SQL SERVER?” in SQL SERVER chapter.

14.How can we perform transactions in .NET?

The most common sequence of steps that would be performed while developing a transactional application is as follows:
• Open a database connection using the Open method of the connection object.
• Begin a transaction using the Begin Transaction method of the connection object. This method provides us with a transaction object that we will use later to commit or rollback the transaction. Note that changes caused by any queries executed before calling the Begin Transaction method will be committed to the database immediately after they execute. Set the Transaction property of the command object to the above mentioned transaction object.
• Execute the SQL commands using the command object. We may use oneormorecommand objects for this purpose, as long as the Transaction property of all the objects is set to a valid transaction object.
• Commit or roll back the transaction using the Commit or Rollback method of the transaction object.
• Close the database connection.

15.What is difference between Dataset? Clone and Dataset. Copy?

Clone: - It only copies structure, does not copy data.
Copy: - Copies both structure and data.

16. What is Maximum Pool Size in ADO.NET Connection String?

Maximum pool size decides the maximum number of connection objects to be pooled. If the maximum pool size is reached and there is no usable connection available the request is queued until connections are released back in to pool. So it’s always a good habit to call the close or dispose method of the connection as soon as you have finished work with the connection object.

17. How to enable and disable connection pooling?
For .NET it is enabled by default but if you want to just make sure set Pooling=true in the connection string. To disable connection pooling set Pooling=false in connection string if it is an ADO.NET Connection. If it is an OLEDBConnection object set OLE DB Services=-4 in the connection string.

Post a Comment