C# Interview Questions for Experienced professionals (Part -2)

1. How to update web.config programatically?

This file is an XML file, and reading/writing XML is supported by .NET. You can choose one of the method as per your requirement:

  • Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size of the document is not too big.
  • Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading
  • Use the class System.Xml.Linq.XDocument; this is to support LINQ to XML Programming.

2. What is the difference between Stack and Heap memory in C#?

3. What is the difference between covariance and contravariance?

4. What are indexers in C# .NET? What is the difference between property & indexers?

An indexer is a pair of get and set accessors, similar to those of properties.

  • Like a property, an indexer does not allocate memory for storage.
  • Both indexers and properties are used primarily for giving access to other data members with which they are associated and for which they provide get and set access.
    − A property usually represents a single data member.
    − An indexer usually represents multiple data members.
  • Like a property, an indexer can have either one or both of the accessors.
  • Indexers are always instance members; hence, an indexer cannot be declared static.
  • Like properties, the code implementing the get and set accessors does not have to be associated with any fields or properties. The code can do anything, or nothing, as long as the get accessor returns some value of the specified type.

 

5. How to create array with non-default value in C#?

You can create array with non-default value in C# using Enumerable.Repeat.

 

6. What’s difference between delegate and events?

An event is just a wrapper for a multicast delegate.
Adding a public event to a class is almost the same as adding a public multicast delegate field.
In both cases, subscriber objects can register for notifications, and in both cases the publisher object can send notifications to the subscribers. However, a public multicast delegate has the undesirable property that external objects can invoke the delegate, something we’d normally want to restrict to the publisher. Hence events – an event adds public methods to the containing class to add and remove receivers, but does not make the invocation mechanism public.

7. Can we Overload main() method in C#?

Yes, We can overload main() method. A C# class can have any number of main() methods.
But to run the C# class, class should have main() method with signature as “public static void main(String[] args)”. If you do any modification to this signature, compilation will be successful. But, You will get run time error as main method not found.

8. What are the differences between Dispose and Finalize ?

Dispose Finalize
It is used to free unmanaged resources at any time. It can be used to free unmanaged resources held by an object before that object is destroyed.
It is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface. It is called by Garbage Collector and cannot be called by user code.
It is implemented by implementing IDisposable interface Dispose() method. It is implemented with the help of Destructors
There is no performance costs associated with Dispose method. There is performance costs associated with Finalize method since it doesn’t clean the memory immediately and called by GC automatically.

9. How to design a class (or data type) which sorts its data either ascending or descending by default

You can do this by using Array.BinarySearch. Below is the implementation in C#.

 

You have a component with 5 parameters and deployed to client side now you changed your method which takes 6 parameters. How can you deploy this without affecting the client’s code?

Instead of adding 6th parameter to the existing method, write new overloaded method with 6 parameters.

So when old application calls this method, method with 5 parameter will execute.
And method with 6 parameter will be used by new application. That way we can provide backward compatibility to old application.

Source:csharpStar.com

Please follow and like us:

Leave a reply

Your email address will not be published.