Avoiding Unreachable Code
When creating methods, you should avoid causing a situation in which a portion of code
cannot, under any circumstances, be executed. This is called
unreachable code,
and it is
considered incorrect in C#. The compiler will issue a warning message if you create a
method that contains unreachable code. For example:
public void MyMeth() {
char a, b;
// ...
if(a==b) {
Console.WriteLine("equal");
return;
} else {
Console.WriteLine("not equal");
return;
}
Console.WriteLine("this is unreachable");
}
Here, the method
MyMeth( )
will always return before the final
WriteLine( )
statement is
executed. If you try to compile this method, you will receive a warning. In general, unreachable
code constitutes a mistake on your part, so it is a good idea to take unreachable code
warnings seriously.
Constructors
In the preceding examples, the instance variables of each
Building
object had to be set
manually using a sequence of statements, such as
www.freepdf-books.com
Do'stlaringiz bilan baham: |