Computer Science C#
Which access modifier is not allowed in a struct? Private Public Protected Internal All of the Above None of the Above A block {…} in C# is a parameter brackets single statement namespace scoping mechanism
Expert Answer
Question 25 :
Answer : Protected
Struct can’t be declared as ‘Protected’
Reasons:
1. structs doesnt support inheritance.
2. structs are mean to be use for immutable(won’t change) objects, more over struct semantics are ‘value type’
Question 26 :
Answer : Scoping Mechanism
A block in c# handles scope.
Example : 1
The below code snippet is allowed. Here we have one outer scope and two inner scopes, in both inner scopes we have same variable if we execute this we dont get any compile error, since we are having two different scopes for variable ‘a’.
{
{
int a = 0;
}
{
int a = 1;
}
}
Example : 2
The below code snippet is not allowed. Here if you see we are declaring and initialising b in inner scope and we are trying to change it in outer scope which gives us compile error while executing. This is called scope mechanism.
{
{
int b = 0;
}
b = 1;
}
SnapShot: