How to Declare a Constant with const in C#
A const is a value that never changes: the maximum number of attempts, a VAT rate, a placeholder text. Write it once under a clear name, and the compiler stops any code from changing it.
General Programming Concepts and Languages
View All TagsA const is a value that never changes: the maximum number of attempts, a VAT rate, a placeholder text. Write it once under a clear name, and the compiler stops any code from changing it.
A partial class is written in pieces: one file for its data, another for its methods, and the compiler joins the pieces into one class. It's how generated code and your own code can share a class without overwriting each other.
namespace Shop.Billing; on the first line of a file puts every type in that file in the Shop.Billing namespace. No braces, and no extra level of indentation for the rest of the file.
Three slashes start a documentation comment: /// <summary>Works out prices after a discount.</summary>. Unlike a normal comment, it has a structure, and the compiler can collect all of them into an XML file next to your program.
Every class and member in C# has an access level: public means any code can use it, private only code in its own class, and internal only code in the same project.
record Book(string Title, string Author, int Year); is a whole class in one line: three properties, a constructor, value-based equality and a ToString that shows the values. Its properties can't change once it's created.
new { Title = "Learning C#", Pages = 320 } creates an object with two properties, without writing a class first. It's the quick way to bundle a few values together, especially inside a LINQ Select.
An object initializer sets properties right where you create the object: new Ticket { Id = "INC1001", Title = "VPN drops" }. A collection initializer does the same for the items of a list or a dictionary.
A [Flags] enum lets one value hold several choices at once, such as read and write access. The bitwise OR operator combines them, and HasFlag checks for one.
Console.WriteLine is how a C# console app shows anything: give it text, a variable or a number, and it prints that followed by a line break. Console.Write does the same without the line break.