On Github RaYell / csharp6-presentation
public class Point { public int _x; public int _y; public int _z; public Person(int x, int y) { this._x = x; this._y = y; } public Person(int x, int y, int z) : this(x, y) { this._z = z; } }
public class Point(int x, int y) { public int _x = x; public int _y = y; public int _z; public Person(int x, int y, int z) : this(x, y) { this._z = z; } }
public class Reservation { public Reservation(string number) { this.Number = number; this.CreatedDate = DateTime.Now; } public DateTime CreatedDate { get; private set; } public string Number { get; private set; } public string User { get { return Principal.WindowsPrincipal.Current.Identity.Name; } } }
public class Reservation(string number) { public DateTime CreatedDate { get; private set; } = DateTime.Now; public string Number { get; private set; } = number; public string User { get; } = Principal.WindowsPrincipal.Current.Identity.Name; }
using System; Console.WriteLine("..."); Console.ReadLine();
using System.Console; WriteLine("..."); ReadLine();
public string GetFieldText(Model model) { if (model == null) { return null; } var field = model.GetField(); if (field == null) { return null; } return field.Text; }
public string GetFieldText(Model model) { return model?.GetField()?.Text; }
int parseResult; if (int.TryParse(parameter, out parseResult)) { // ... }
if (int.TryParse(parameter, out int parseResult)) { // ... }
if (int.TryParse(parameter, out var parseResult)) { // ... }
public string Text { get { return string.Format("{0} {1}", this.Process, this.Message); } } public override ToString() { return string.Format("{0} {1}", this.FirstName, this.LastName); }
public string Text => string.Format("{0} {1}", this.Process, this.Message) public override ToString() => string.Format("{0} {1}", this.FirstName, this.LastName);
try { DoSomething(); } catch (Exception ex) { if (ex.Message.Contains("Fatal")) { Console.WriteLine("Fatal Exception"); } else { throw; } }
try { DoSomething(); } catch (Exception ex) if (ex.Message.Contains("Fatal")) { Console.WriteLine("Fatal Exception"); }
try { await DoSomething(); } catch (Exception ex) { await LogService.Error("Exception", ex); // compiler error throw; } finally { await Logservice.Info("Finished"); // compiler error }
ExceptionDispatchInfo capturedException = null; try { await DoSomething(); } catch (Exception ex) { capturedException = ExceptionDispatchInfo.Capture(ex); } if (capturedException != null) { await LogService.Error("Exception", capturedException.SourceException); await LogService.Info("Finished"); // simulates finally capturedException.Throw(); } await LogService.Info("Finished"); // simulates finally
try { await DoSomething(); } catch (Exception ex) { await LogService.Error("Exception", ex); throw; } finally { await Logservice.Info("Finished"); }
public void Check(Person person) { if (person == null) { throw new ArgumentNullException("person"); } }
public void Check(Person person) { if (person == null) { throw new ArgumentNullException(GetVariableName(() => person)); } } private static string GetVariableName<T>(Expression<Func<T>> expr) { var body = (MemberExpression)expr.Body; return body.Member.Name; }
public void Check(Person person) { if (person == null) { throw new ArgumentNullException(nameof(person)); } }