Posts

C#'s Approach to Inheritance: Interfaces and Multiple Inheritance

Q1: What is multiple inheritance, and why doesn’t C# support it? Multiple inheritance is a feature in some programming languages that allows a class to inherit properties and behaviors from more than one parent class. It introduces complexity and the potential for the “diamond problem,” where ambiguities arise when a class inherits from two classes with a common ancestor. Why C# avoids it: C# deliberately avoids multiple inheritance with classes to prevent complications and ambiguity. The language encourages a safer and more flexible approach using interfaces. Q2: What is the role of interfaces in C#? Interfaces in C# are contracts that define a set of methods, properties, and events. A class implementing an interface commits to providing concrete implementations for all declared members. C# allows a class to implement multiple interfaces. Q3: How do interfaces provide flexibility in C#? Flexibility with interfaces: Interfaces allow a class to implement multiple contracts, o

What is Static Constructor ?

It is a special type of constructor, introduced with C#. It gets called before the creation of the first object of a class(probably at the time of loading an assembly). See example below. public class SomeClass ( ) { static SomeClass ( ) { //Static members may be accessed from here //Code for Initialization } }

How do I calculate a MD5 hash from a string?

It is a common practice to store passwords in databases using a hash. MD5 (defined in RFC 1321 ) is a common hash algorithm, and using it from C# is easy. Here’s an implementation of a method that converts a string to an MD5 hash, which is a 32-character string of hexadecimal numbers. public string CalculateMD5Hash ( string input ) { // step 1, calculate MD5 hash from input MD5 md5 = System . Security . Cryptography . MD5 . Create ( ) ; byte [ ] inputBytes = System . Text . Encoding . ASCII . GetBytes ( input ) ; byte [ ] hash = md5 . ComputeHash ( inputBytes ) ; // step 2, convert byte array to hex string StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < hash . Length ; i ++ ) { sb . Append ( hash [ i ] . ToString ( "X2" ) ) ; } return sb . ToString ( ) ; } An example call: string hash = CalculateMD5Hash ( "abcdefghijklmnopqrstuvwxyz" ) ; …returns a s

What is the difference between const and static readonly?

When dealing with constants in C#, it’s essential to understand the difference between static readonly and const fields. Both are used to represent values that shouldn’t change during the program’s execution, but they have distinct characteristics. const Fields const fields are set to a compile-time constant and cannot be modified afterward. They are implicitly static and are implicitly readonly . The value must be known at compile time. const fields are implicitly static , meaning they belong to the type rather than an instance. They are set during compilation and can’t be changed at runtime. Commonly used for primitive types and strings. class Program { public const int MaxValue = 100 ; static void Main ( string [ ] args ) { // Error: A constant value cannot be modified // MaxValue = 200; } } static readonly Fields static readonly fields are set at runtime and can be modified only in the variable declaration or w

What’s the difference between System.String and System.StringBuilder classes?

While System.String is immutable, meaning its value cannot be changed once it’s created, System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed. StringBuilder is more efficient for scenarios where you need to make many modifications to a string, as it avoids the overhead of creating a new string instance for each change.

Does C# support multiple inheritance?

No, use interfaces instead.

Can you inherit multiple interfaces?

Yes, why not. Through Interface we can achieve