```csharp internal class Program { static void Main(string[] args) { Book book = new Book(); book.Title = "Test"; book.Price = 10; //Console.WriteLine((book.Title, book.Price)); book.Display(); } } class Book { public string Title; public decimal Price; //method public void Display() => Console.WriteLine((Title, Price)); } //========================= //overloading class Person { public string SayHi() => "Hi..."; public string SayHi(string name) => $"Hi {name} "; } Person person = new Person(); Console.WriteLine(person.SayHi()); Console.WriteLine(person.SayHi("Mary")); //========================= //property private decimal price; public decimal Price { get => price; set => price = value > 0 ? value : 0; } //=========================== //read-only property public DateTime Time1=DateTime.Now; //field public DateTime Time2 { // read-only property get=>DateTime.Now; } public DateTime Time3=>DateTime.Now; //read-only property //=========================== //auto property //propfull //private string title; //field //public string Title //property //{ // get { return title; } // set { title = value; } //} //prop public string Title { get; set; } //============================= Console.WriteLine(book.Time1); Console.WriteLine(book.Time2); Console.WriteLine(book.Time3); Console.WriteLine(book.Time4); Thread.Sleep(3000); Console.WriteLine("============================"); Console.WriteLine(book.Time1); Console.WriteLine(book.Time2); Console.WriteLine(book.Time3); Console.WriteLine(book.Time4); public DateTime Time1 = DateTime.Now; //field public DateTime Time2 { // read-only property get => DateTime.Now; } public DateTime Time3 => DateTime.Now; //read-only property public DateTime Time4 { get; set; } = DateTime.Now; //======================== //partial class partial class Customer { public string Name { get; set; } } partial class Customer { public string Address { get; set; } } Customer customer = new Customer(); customer.Name = "Xxx"; customer.Address = "yyy"; Console.WriteLine((customer.Name, customer.Address)); //============================= //object initializer Book book = new Book { Title="C#" }; book.Display(); //============================= //struct struct ContactInfo { public string Name; public string Title; public string Phone; public void Display() => Console.WriteLine($"{Name}:{Title}:{Phone}"); } ContactInfo customer; customer.Name = "王小明"; customer.Title = "先生"; customer.Phone = "(04)1234-5678"; customer.Display(); ////============================= //class vs struct ContactInfo customer = new ContactInfo(); //ContactInfo customer; customer.Name = "王小明"; customer.Title = "先生"; customer.Phone = "(04)1234-5678"; ContactInfo customer2 = customer; customer2.Name = "Xxx"; customer.Display(); Console.WriteLine("===================="); customer2.Display(); ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up