Most Commonly Asked C# Questions & Answers

Here are some of the most commonly asked C# questions and answers, curated from some of the most reputed platforms like StackOverflow, Quora, and Github where developers, who work in real-time environment provide their valuable inputs.

C# at its very core is derived from the C programming language and it has features such as garbage collection allowing beginners to become proficient in C# more quickly than in C or C++. The language is object-oriented, a feature very similar to Java. C# comes with an extensive class library, and supports exception handling, multiple types of polymorphism, and separation of interfaces from implementations.

Some of these features, when combined with the powerful development tools that it offers, multi-platform support, and generics, make C# a good choice for many types of software development projects for a C# development company. It provides great support for rapid application development projects, projects implemented by individuals or large or small teams, Internet applications, and projects with strict reliability requirements.

Regarding of the programming language or technology, the learning curve is always steep. With new advancements and additions, those individuals who work with the technology need to keep themselves updated. In the long marathon, the participants can easily miss a few aspects along the way.

Here are some of the most commonly asked C# questions and answers, curated from some of the most reputed platforms like StackOverflow, Quora, and Github where developers, who work in real-time environment provide their valuable inputs.

Question 1


In most programming languages, dictionaries are preferred over hashtables. What are the reasons behind that?


Answer: For what it's worth, a Dictionary is (conceptually) a hash table.


If the question means why do we use the DictionaryTKey, TValue class instead of the Hashtable class?, then it's an easy answer: DictionaryTKey, TValue is a generic type, Hashtable is not. That means you get type safety with DictionaryTKey, TValue, because you can't insert any random object into it, and you don't have to cast the values you take out.


Interestingly, the DictionaryTKey, TValue implementation in the .NET Framework is based on the Hashtable.

Dictionary Hashtable differences

  • Generic Non-Generic
  • Needs own thread synchronization Offers thread safe version through Synchronized() method
  • Enumerated item: KeyValuePair Enumerated item: DictionaryEntry
  • Newer ( .NET 2.0) Older (since .NET 1.0)
  • is in System.Collections.Generic is in System.Collections
  • Request to non-existing key throws exception Request to non-existing key returns null
  • potentially a bit faster for value types bit slower (needs boxing/unboxing) for value types
    Dictionary / Hashtable similarities:
  • Both are internally hashtables == fast access to many-item data according to key
  • Both need immutable and unique keys
  • Keys of both need own GetHashCode() method
  • Similar .NET collections (candidates to use instead of Dictionary and Hashtable):
  • ConcurrentDictionary - thread safe (can be safely accessed from several threads concurrently)
  • HybridDictionary - optimized performance (for few items and also for many items)
  • OrderedDictionary - values can be accessed via int index (by order in which items were added)
  • SortedDictionary - items automatically sorted
  • StringDictionary - strongly typed and optimized for strings

Question 2


In C#, what makes a field different from a property, and when should a field be used instead of a property?
Answer: Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.


public class MyClass
{
// this is a field. It is private to your class and stores the actual data. private string _myField;
// this is a property. When accessed it uses the underlying field, // but only exposes the contract, which will not be affected by the underlying field


public string MyProperty
{
Get
{
return _myField;
}
Set
_myField = value;
}
}
// This is an AutoProperty (C# 3.0 and higher) - which is a shorthand syntax // used to generate a private field for you
public int AnotherProperty{get;set;}
}


An important difference is that interfaces can have properties but not fields. This, to me, underlines that properties should be used to define a class's public interface while fields are meant to be used in the private, internal workings of a class. As a rule I rarely create public fields and similarly I rarely create non-public properties.

Question 3


How can I generate a random 8 character alphanumeric string in C#?


Answer: I heard LINQ is the new black, so here's my attempt using LINQ:


private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s = s[random.Next(s.Length)]).ToArray());
}

Conclusion


Learning programming languages is not as hard as sustaining and developing the knowledge that you have acquired. You gain knowledge by practising, asking the right questions directed towards right platforms, and far more than that, by making mistakes and improving on them. In your ordeal to hire C# developers for your development project, make sure how enthusiastic they are to learn and adapt, rather than using the pre-existing knowledge.

521 Views