Chuyên gia Bill Wagner đã có bài phát biểu về cách chúng ta phải thay đổi thói quen lập trình để khai thác triệt để những tính năng tiên tiến trong C# (ở đây là C# 8) tại hội thảo NDC Conferences
Công cụ và tài liệu tham khảo
- Ngôn ngữ C# 8.0
- IDE: Visual Studio 2019
- Tài liệu: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8
Một vài code minh họa
Dùng Tuples (7:00)
Tham khảo: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#tuple-patterns
Truyền thống:
public Point (double x, double y){ this.x = x; this.y = y; distance = default; }
Viết lại:
public Point (double x, double y) => (this.x, this.y, distance) = (x, y, default);
Truyền thống
public static bool operator == (Point left, Point right) => left.X == right.X && left.Y == right.Y;
Viết lại
public static bool operator == (Point left, Point right) => (left.X, left.Y) == (right.X, right.Y);
Truyền thống
public static bool operator != (Point left, Point right) => left.X != right.X || left.Y != right.Y;
Viết lại
public static bool operator != (Point left, Point right) => (left.X, left.Y) != (right.X, right.Y);
Truyền thống
public void SwapCoords(){ var temp = X; X = Y; Y = temp; }
Viết lại
public void SwapCoords() => (X, Y) = (Y, X);
Dùng readonly cho struct và các thành viên bên trong struct (13:30)
Tham khảo: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#readonly-members
public struct Point{ private double x; public double X{ get => x; private set => x = value; } }
Viết lại dùng readonly
public readonly struct Point{ public double X{ get ; } }
Kiểm tra Nullable (22:49)
Truyền thống
public override bool Equals(object? obj){ if(obj is Point){ var otherPT = (Point)obj; return this == otherPT; }else{ return false; } }
Viết lại lần 1
public override bool Equals(object? obj){ if(obj is Point otherPT){ return this == otherPT; }else{ return false; } }
Viết lại lần 2
public override bool Equals(object? obj) => (obj is Point otherPT) ? this == otherPT : false;
Toán tử ??
Tham khảo: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nullcoalescing-operator
Truyền thống
class Person{ private string _firstName; public string FirstName{ get => _firstName; set{ if(value == null) throw new ArgumentNullException(paramName: nameof(value), message: "Can not set name to null"); _firstName = value; } } }
Viết lại
set => _firstName = value ?? throw new ArgumentNullException (paramName: nameof(value), message: "Can not set name to null");
Truyền thống
public string HydpenateForPartner(Person partner){ if(partner != null){ return $"{partner.LastName} – {this.LastName}"; } throw new ArgumentNullException(paramName: nameof(partner), message: "Partner should not be null"); }
Viết lại
public string HydpenateForPartner(Person partner){ _= partner ?? throw new ArgumentNullException(paramName: nameof(partner), message: "Partner should not be null"); return $"{partner.LastName} – {this.LastName}"; }
Paterns and Data (34:34)
Tham khảo: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/pattern-matching
Ý kiến bài viết