.NET - .NET Programming Concepts

9.
What does a break statement do in the switch statement?
The switch statement is a selection control statement that is used to handle multiple choices and transfer control to the case statements within its body. The following code snippet shows an example of the use of the switch statement in C#:
switch(choice)
{
 case 1:
 console.WriteLine("First"); 
 break;
 case 2:
 console.WriteLine("Second");
 break;
 default:
 console.WriteLine("Wrong choice");
 break;
}

In switch statements, the break statement is used at the end of a case statement. The break statement is mandatory in C# and it avoids the fall through of one case statement to another.

10.
Explain keywords with example.
Keywords are those words that are reserved to be used for a specific task. These words cannot be used as identifiers. You cannot use a keyword to define the name of a variable or method. Keywords are used in programs to use the features of object-oriented programming.

For example, the abstract keyword is used to implement abstraction and the inherits keyword is used to implement inheritance by deriving subclasses in C# and Visual Basic, respectively.

The new keyword is universally used in C# and Visual Basic to implement encapsulation by creating objects.

11.
Briefly explain the characteristics of value-type variables that are supported in the C# programming language.
The variables that are based on value types directly contain values. The characteristics of value-type variables that are supported in C# programming language are as follows:
  • All value-type variables derive implicitly from the System.ValueType class
  • You cannot derive any new type from a value type
  • Value types have an implicit default constructor that initializes the default value of that type
  • The value type consists of two main categories:
    • Structs - Summarizes small groups of related variables.
    • Enumerations - Consists of a set of named constants.


12.
Give the syntax of using the while loop in a C# program.
The syntax of using the while loop in C# is:
while(condition) //condition
{
 //statements
}
You can find an example of using the while loop in C#:
int i = 0;
while(i < 5)
{
 Console.WriteLine("{0} ", i);
 i++;
}

The output of the preceding code is: 0 1 2 3 4 .