5 comparisons in C# Object Oriented Programming you must revise before a Technical Interview — Part 2
C# is one of the most popular Object Oriented Programming languages. Together with .NET, it is arguably one of the most feature rich languages that you can use to build applications.
As a developer, the following are 5 key comparisons that you must know before going for your next technical interview in C# .NET
You can find the Part 1 of this series here — 5 comparisons in C# Object Oriented Programming you must revise before a Technical Interview — Part 1
IEnumerable vs IEnumerator vs IQueryable
The IEnumerator interface comes from the System.Collections namespace. It helps us navigate through the contents of a collection.
When we use a foreach loop to go through a collection, we’re actually using the specific IEnumerator set up by that collection.
On the other hand, the IEnumerable interface represents a group of items we can move through one by one.
It has just one method called GetEnumerator, which gives us an IEnumerator object to help us move through the items.
The IQueryable interface is a unique collection that lets us retrieve and manage data from an external source, like a database.
It offers us an IQueryProvider, which creates LINQ providers that help with tasks like LINQ-to-SQL, which are all about working with data.
Comparison — https://referbruv.com/blog/csharp-ienumerable-vs-ienumerator-vs-iqueryable-ultimate-guide/
const vs readonly
We generally use const variables to store unchanging values, like constants in math or fixed numbers. When we declare a const variable, we must give it a value right away and can’t change it later.
On the other side, readonly variables can also hold constant values. We set their value when we declare them or in the class’s constructor.
What’s special is that we can set the readonly value differently for each instance, but only when we’re creating the instance using the constructor.
Comparison — https://referbruv.com/blog/easy-differences-between-const-and-readonly-in-csharp/
ref vs out
We use the ref keyword for “call by reference.” It passes a value type’s reference from our method to another method.
The other method can read and change the initial value, and these changes are seen outside the method.
In contrast, the out keyword only sends data out of the method, and that value can be used outside. We use it when we want a method to give us multiple values back.
Comparison — https://referbruv.com/blog/easy-differences-between-ref-and-out-keywords-in-csharp/
Property vs Variable
A variable is like a named storage space that holds a specific kind of information. We can use it to store and retrieve data, depending on where we define it.
When we create a variable, we pick its type and can also choose if it’s public, private, or something else.
Usually, classes have private variables and public methods to work with them. But we have a simpler way called properties. Properties wrap up the variable and its methods into a neat package.
Behind the scenes, there’s a real variable storing the data, but we don’t directly use that variable — we use the property instead.
Comparison — https://referbruv.com/blog/what-are-the-differences-between-a-property-and-a-variable/
String vs StringBuilder
A string is a lineup of characters that we use to hold text. In C#, it’s a type of reference stored in a heap memory. Strings don’t change once we make them — they stay the same.
This makes sure they work well when multiple things are happening at once. When we want to change a string a lot, we use something called StringBuilder.
With StringBuilder, we can alter the text without making a bunch of new strings. This saves memory and makes things faster.
Comparison — https://referbruv.com/blog/easy-differences-between-string-and-stringbuilder-in-csharp/