Today we will continue the series of questions you may encounter during an interview for the position of junior .NET developer. We will analyze the next 10 questions with their answers.
21) What are access modifiers in C#?
In C# we have 5 access modifiers. These are public, private, protected, internal and protected internal.
Public modifier – an element marked with this modifier is available everywhere, from anywhere inside and outside the class.
Private modifier – an element marked with this modifier is available only in the same class in which it was created.
Protected modifier – an element marked with this modifier is available only in the class in which it was created, as well as in derived classes. That is, classes that inherit from the class in which this element was declared.
Internal modifier – availability for a given element only in the same module.
Protected internal modifier – mark accessibility for everyone in a given module and for each derived class. Is the sum of protected and internal accesses.
private string _pesel;
public string FirstName;
protected string LastName;
protected internal int Age;
internal DateTime DateOfBirth;
22) What is the difference between const and readonly?
The value of a field marked as const cannot be changed while the program is running. This is a constant whose value is determined at compile time. It applies to values that never change. For example, the value of PI. A field marked with the const modifier is treated as a static field. In turn, a field value marked as readonly can be assigned both at compile time and at runtime, but only at initialization and in the constructor. Also, unlike const, it can be an instance value, not just static.
private readonly string _name;
public const double PI = 3.14159;
23) What is the default class access modifier?
The default class access modifier in C# is internal.
class Student
{
}
internal class Student
{
}
24) What is the default access modifier for class members?
The default access modifier for class members is private.
int _age;
private string _pesel;
void DisplayAge()
{
}
private void DisplayPesel()
{
}
25) Is it possible to inherit from multiple classes in C#?
In C# we can only inherit from 1 class. It is not possible to inherit from several classes.
public class Student : Person
{
}
26) Can a class implement multiple interfaces?
Yes. Unlike inheritance, in C# a class can implement multiple interfaces.
public class Student : IDisposable, ICloneable
{
public object Clone()
{
return null;
}
public void Dispose()
{
}
}
27) Is it possible to create a method in C# that can take a variable number of parameters?
Yes, to do this you need to use the params keyword.
public class Student
{
private List<int> _marks = new List<int> ();
public void AddMarks(params int[] number)
{
foreach (var item in number)
{
_marks.Add(item);
}
}
}
var student = new Student();
student.AddMarks(1, 2, 3);
student.AddMarks(1, 2, 3, 4, 5, 5, 5, 5, 1);
28) What is the difference between the ref and out keywords?
Both a parameter marked with the ref and out keywords allow us to pass a value to a method by reference. However, each of them will be more suited to a different situation. Basic differences between them:
-A variable marked with the word ref must be initialized before being passed as a parameter.
-A variable marked with the word out does not have to (but can) be initialized before being passed as a parameter. There may also be initialization before forwarding, but it doesn't make any sense.
-A variable marked by ref does not need to be initialized in the method.
-A variable marked with out must be initialized in the method.
To summarize, a variable marked with ref tells us that the variable has already been initialized and can be read and/or changed in the method, and all changes will be visible outside the method as well. A variable marked with out tells us that the variable has not been initialized yet, it has no value. Its value will be initialized inside the method and we usually use it in cases where we want the value of some variable to be changed outside the method.
public void IncrementRefValue(ref int value)
{
value++;
}
public void IncrementOutValue(out int value)
{
value = 10;
value++;
}
29) What does the void keyword mean?
A method can be marked with such a keyword. And it tells us that the method does not return any value.
public void Display()
{
}
30) What are indexers?
Indexers are methods that allow us to access an object as if it were an array or collection of elements.
public class MyList
{
private List<int> _list = new List<int> ();
public int this[int index]
{
get
{
return _list[index];
}
set
{
_list[index] = value;
}
}
}
var myList = new MyList();
myList[0] = 1;
TERMINATION
Those are all the questions for today. We will return to further questions in the next article. If you like this material, be sure to join my community - free registration, where you will also have access to additional materials.
That's all for today, see you in the next article.