28
loading...
This website collects cookies to deliver better user experience
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
System.Console.WriteLine("Hello World!");
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public Book()
{
}
public Book(string title, string author)
{
Title = title;
Author = author;
}
}
var book = new Book();
// or
Book book = new Book();
Book book2 = new();
Book book3 = new("1", "A1");
Book
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
}
var book = new Book { Author = "1", Title = "2" };
book.Title = "2";
C# 9
Init-only
feature comes into play:public class Book
{
public string Title { get; init; }
public string Author { get; init; }
}
var book = new Book { Author = "1", Title = "2" };
book.Title = "2"; // compile error
public class Book{ public string Title { get; set; } public string Author { get; set; } public static decimal Postage(decimal price) => price switch { < 20 => 6.99m, >= 20 and < 40 => 5.99m, >= 40 and < 60 => 2.99m, _ => 0 };}
public class Book{ public string Title { get; } public string Author { get; } public Book(string title, string author) { Title = title; Author = author; }}
var book = new Book("Title1", "Author1");
var json = JsonSerializer.Serialize(book);Console.WriteLine(json);var book2 = JsonSerializer.Deserialize<Book>(json);var isEqual = book == book2;Console.WriteLine($"book == book2: {isEqual}"); // false
public static bool operator ==(Book left, Book right) => left is object ? left.Equals(right) : right is null;
==
operator we also have to override !-
operator:public static bool operator !=(Book left, Book right) => !(left == right);
Equals
method:public override bool Equals(object obj) => obj is Book b && Equals(b);public bool Equals(Book other) => other is object && Title == other.Title && Author == other.Author;
GetHashCode
and ToString
methods.public override int GetHashCode() => HashCode.Combine(Title, Author);public override string ToString() => $"{Title} - {Author}";
IEquatable<T>
interface.public class Book : IEquatable<Book>
public class Book : IEquatable<Book>{ public string Title { get; } public string Author { get; } public Book(string title, string author) { Title = title; Author = author; } public static bool operator ==(Book left, Book right) => left is object ? left.Equals(right) : right is null; public static bool operator !=(Book left, Book right) => !(left == right); public override bool Equals(object obj) => obj is Book b && Equals(b); public bool Equals(Book other) => other is object && Title == other.Title && Author == other.Author; public override int GetHashCode() => HashCode.Combine(Title, Author);}
Console.WriteLine($"book == book2: {isEqual}");
will output true
.C# 9
we can use the record
type for the same behavior. It allows making behavior for classes as if they were structures.record Book(string Title, string Author)
public partial class Book{ public string Title { get; set; } public string Author { get; set; } public decimal Price { get; set; } private partial decimal SetPrice();}public partial class Book{ private partial decimal SetPrice() { return 0m; }}
C# 9
we can return derived types in overridden methods.public class Book{ public string Title { get; set; } public string Author { get; set; }}public class CollectionBook : Book{ public string Edition { get; set; }}public abstract class BookService{ public abstract Book GetBook();}public class CollectionBookService : BookService{ public override CollectionBook GetBook() { return new CollectionBook(); }}