| internal class Program
|
| {
|
|
|
| class ExampleError
|
| {
|
| public string Name { get; set; }
|
| public ExampleError(string name)
|
| {
|
| if (name != null)
|
| Name = name;
|
| else
|
| throw new ArgumentNullException(nameof(name));
|
| }
|
| }
|
|
|
| static void Main(string[] args)
|
| {
|
| try
|
| {
|
| ExampleError example = new ExampleError("Normal Name");
|
| ExampleError example2 = new ExampleError(null);
|
| }catch(Exception e)
|
| {
|
| Console.WriteLine(e.Message);
|
| }
|
| }
|
| }
|
| namespace L19
|
| {
|
| internal class Program
|
| {
|
|
|
| class DisposeExample : IDisposable
|
| {
|
| private bool isDisposed = false;
|
|
|
| public void Do()
|
| {
|
| Console.WriteLine("Действия класса");
|
| }
|
|
|
| private void Clean(bool disposing)
|
| {
|
| if (!isDisposed)
|
| {
|
| if (disposing)
|
| {
|
| Console.WriteLine($"Освобождение управляемых ресурсов {this}...");
|
| }
|
| Console.WriteLine($"Освобождение неуправлямых ресурсов {this}...");
|
| }
|
| isDisposed = true;
|
| }
|
|
|
| public void Dispose()
|
| {
|
| Clean(true); //true, если очистка инициирована владельцем объекта
|
| GC.SuppressFinalize(this);
|
| }
|
|
|
| ~DisposeExample()
|
| {
|
| Clean(false);//false, если очищается сборщиком мусора
|
| }
|
| }
|
|
|
| class ExampleError
|
| {
|
| public string Name { get; set; }
|
| public ExampleError(string name)
|
| {
|
| if (name != null)
|
| Name = name;
|
| else
|
| throw new ArgumentNullException(nameof(name));
|
| }
|
| }
|
|
|
| static void Main(string[] args)
|
| {
|
| /*IDisposable disposable = new DisposeExample();
|
| disposable.Dispose();*/
|
| /*DisposeExample example = new DisposeExample();
|
| try
|
| {
|
| example.Do();
|
| }
|
| catch(Exception ex)
|
| {
|
| Console.WriteLine(ex.Message);
|
| }
|
| finally
|
| {
|
| example.Dispose();
|
| }*/
|
| using (DisposeExample example = new DisposeExample())
|
| {
|
| example.Do();
|
| }
|
| }
|
| }
|
| }
|