C# Q&A : Part 2
Tags: dotnet, csharp, beginners
Published: 2023-02-14 14:50:42

Alrighty!
Welcome to Part 2 from this 2 parts series , where we answer C# questions with theory and code examples if required , in the first part , we've discussed 10 questions , and here we are again to discuss 10 more.


## 1: What're the main concepts of **OOP** ?
In Object Oriented Programming , we have 4 essential concepts , also known as the pillars of OOP , which you can see listed here:

**1:** Encapsulation
**2:** Abstraction
**3:** Inheritance
**4:** Polymorphism


**NOTE**: I have an in depth article on Object Oriented Programming where I break down all the theory and explain all the nitty gritty details of OOP concepts accompanied with examples , so if you're looking for a deeper dive in OOP , you can check it out here -> https://dev.to/rasheedmozaffar/introducing-object-oriented-programming-in-c-5b1o


## 2: Why do we need **access modifiers** ?
Access modifiers control the accessibility to members by either fully exposing the members to all the classes outside , or by keeping things accessible only within the same class , or maybe hiding the members thoroughly from other classes , because some times we don't want unauthorized and unwanted change from the outside of our classes , so in order to prevent that unwanted access that could change the behavior of our class or its members , we use access modifiers.

## 3: What're the available **access modifiers in C#** ?
In C# , we have **6** access modifiers:
**1:** public
**2:** private
**3:** internal
**4:** protected
**5:** protected internal
**6:** private internal

## 4: What's an **enum** ?
An enum or **Enumeration** is a value type that can store named integral constants , things that can be numbered and also represented by text , a good example would be days of the week.
While adding the values inside an enum , keep in my mind that counting starts from **0** , which means the first value will be 0 , second is 1 and so on , but it's changeable though  , you can do it like that:

```


public enum WeekDays 
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7
}

```

## 5: What's a **sealed** class ?
When we declare a class as sealed , it implies that this class cannot be inherited by other classes , therefore we won't be able to derive classes from that class which is marked as a sealed class.


## 6: What's an **abstract** class ?
Abstract classes are contradictory to sealed classes , sealed classes cannot be inherited , while abstract classes must be inherited , let's look at a brief definition of the **abstract modifier** according to **Microsoft Docs**:

> The abstract modifier indicates that the thing being modified has a missing or incomplete implementation.

The **abstract modifier** doesn't necessarily have to be applied on classes , instead , it can be used with classes , methods , properties , indexers and events.

## 7: What's a **partial class** ?
C# allows dividing a single C# class definition into multiple files , so you can keep the code more structured and less chaotic by reducing the amount of code written in a single file and distributing the code on multiple files , this can be achieved by using the **partial** keyword , like this:

```

public partial class MyPartialClass
{
   //Class Members Go Here.
}

```

## 8: What's an **array** ?
An array is essentially an object that can hold a fixed number of elements of the same type , however , the array size as mentioned in the previous line , is fixed and is defined by the developer while creating the array instance , this makes arrays not the most favorable choice among other collections because the size is fixed and must be known beforehand , and since the size is fixed , you can't remove or add new elements to the array after initialization

## 9: How elements are accessed inside an **array** ?
Array elements are indexed , which means every element has an index that represents it , by calling the array and using an index , you can retrieve the element that corresponds to that index , arrays in C# are like many programming languages , are **0 indexed**  , which indicates that the indexing always starts at **0** , so for that , the first element will be at index position 0 , and the last element will be at index position **n - 1** , **n** here representing the length of the array.


## FINAL QUESTION

## 10: What's an **arraylist** , and should you use it ?
Array list or a dynamic array is an array that can store values of all types without complaining , in C# , it's called an **array list** , which is a **non-generic collection** that has a non-fixed size which can grow or shrink based on the number of elements residing in it , plus , it can perform what's known as **Boxing** and **Unboxing** on the elements it stores , but what do these 2 terms even mean ?
**Boxing**: Is the conversion from any value type like integers , chars , floats etc... , to a reference type (**Object**) or **Pointer Types**

```

int i = 10;
object o = i;

```
By doing this , we are converting the integer value to reference type or object , which is known as boxing.

**Unboxing:** Is the process of reverting from a reference type(**Object**) to a value type , and it's done through using an explicit cast , like this:

```

object o = 10;
int i = (int)o;

```
Here , we are using an **explicit cast** to cast the object to an integer value type , which is known as unboxing.

Arraylists treat any element as an object , but , that versatility has a catch , which is performance drawbacks , by boxing every element that's passed to it and then unbox it when needed , it's using computational power to perform these operations , thus , hence it's highly advised to not use the array list type anymore and rather use the more performant generic collections like the generic list `List<T>` which provides performance gains and type checking at compile time , thus the compiler will complain in case you attempted to insert elements to the list that aren't of a suitable type to the one you've picked.


## Final words
In this series we've covered 20 questions from totally different levels , I hope you found these questions useful and I hope you learned something new either from the theoretical parts or the coding examples.

If you liked that kind of posts , please let me know so I can write more of them when I can.