Guide to the Language


Download 2 Mb.
Pdf ko'rish
bet54/78
Sana30.04.2023
Hajmi2 Mb.
#1414515
TuriGuide
1   ...   50   51   52   53   54   55   56   57   ...   78
Bog'liq
C sharp

 Using Statement
The using statement provides a simpler syntax for writing the try-finally 
statement. This statement starts with the using keyword followed by the 
resource to be acquired, specified in parentheses. It then includes a code 
block in which the obtained resource can be used. When the code block 
finishes executing, the Dispose method of the object is automatically 
called to clean it up. This method comes from the System.IDisposable 
interface, so the specified resource must implement this interface. The 
following code performs the same function as the one in the previous 
example, but with fewer lines of code.
using System.Drawing;
// ...
void using (Bitmap b = new Bitmap(100, 50)) {
System.Console.WriteLine(b.Width); // "100"
} // disposed
C# 8.0 simplified resource management further by allowing for using 
declarations. This removes the need for the curly brackets as the resource 
handler will automatically be disposed of when it goes out of scope.
Chapter 21 exCeption handling


127
void MyBitmap()
{
using Bitmap b = new Bitmap(100, 50);
System.Console.WriteLine(b.Height); // "50"
} // disposed
 Throwing Exceptions
When a situation occurs that a method cannot recover from, it can 
generate an exception to signal the caller that the method has failed. This 
is done using the throw keyword followed by a new instance of a class 
deriving from System.Exception.
static void MakeError()
{
throw new System.DivideByZeroException("My Error");
}
The exception will then propagate up the caller stack until it is caught. 
If a caller catches the exception but is not able to recover from it, the 
exception can be rethrown using only the throw keyword. If there are no 
more try-catch statements, the program will stop executing and display 
the error message.
static void Main()
{
try {
MakeError();
}
catch {
throw; // rethrow error
}
}
Chapter 21 exCeption handling


128
As a statement, the throw keyword cannot be used in contexts that 
require an expression, such as inside a ternary statement. C# 7.0 changed 
this by allowing throw to also be used as an expression. This expands 
the locations from which exceptions may be thrown, such as inside the 
following null- coalescing expression.
using System;
class MyClass
{
private string _name;
public string name
{
get => _name;
set => _name = value ?? throw new
ArgumentNullException(nameof(name)+" was null");
}
static void Main()
{
MyClass c = new MyClass();
c.name = null; // exception: name was null
}
}
Note the use of the nameof expression here, which was introduced 
in C# 6.0. This expression turns the symbol inside the parentheses into a 
string. The benefit of this shows itself if the property is renamed, as the 
IDE can then find and rename this symbol. This would not be the case if a 
string had been used instead.
Chapter 21 exCeption handling


129
© Mikael Olsson 2020 
M. Olsson, C# 8 Quick Syntax Reference
https://doi.org/10.1007/978-1-4842-5577-3_22

Download 2 Mb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   78




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling