Saturday, March 7, 2026

C# 12 and .NET 8 note

C# 12 and .NET 8 1. https://github.com/markjprice/cs12dotnet8 2. The companion book, Apps and Services with .NET 8 3. code --list-extensions Visual Studio Code Extensions: C# Dev Kit 4. dotnet --list-sdks dotnet --info dotnet new console --use-program-main dotnet run 5. namespace doesn't require the folder structure like Java, can remove namespace com.t1; and ran it directly dotnet new sln --name Chapter01 dotnet new console --output HelloCS dotnet sln add HelloCS dotnet help new 6. Console.WriteLine($"Namespace: {name}"); Console.WriteLine("Value is {0}",19.8); #error version 7. Scott Hanselman from Microsoft has an excellent YouTube channel about computer stuff they didn’t teach you at school: http://computerstufftheydidntteachyou.com/. 8. C# keywords use all lowercase. Although you can use all lowercase for your own type names, you should not. With C# 11 and later, the compiler will give a warning if you do. 9. You must prefix it with the @ symbol to use a verbatim literal string, as shown in the following code: string filePath = @"C:\televisions\sony\bravia.txt"; 10. Raw string literals start and end with three or more double-quote characters, as shown in the following code: string xml = """ Mark """; 11. float realNumber = 2.3f; double anotherRealNumber = 2.3; The double type is not guaranteed to be accurate decimal c = 0.1M; // M suffix means a decimal literal value decimal d = 0.2M; object height = 1.88; // Storing a double in an object. object name = "Amir"; // Storing a string in an object. int length2 = ((string)name).Length; dynamic something;//has impact on performance Console.WriteLine($"something is a {something.GetType()}"); 12. Local variables are declared inside methods, and they only exist during the execution of that method. Once the method returns, the memory allocated to any local variables is released. This happens at compile time so using var has no effect on runtime performance. 13. The Debug class is used to add logging that gets written only during development. The Trace class is used to add logging that gets written during both development and runtime. 14. Constant: The data never changes. The compiler literally copies the data into any code that reads it. Read-only: The data cannot change after the class is instantiated, but the data can be calculated or loaded from an external source at the time of instantiation. 15. private The member is accessible inside the type only. This is the default. internal The member is accessible inside the type and any type in the same assembly. protected The member is accessible inside the type and any type that inherits from the type. C# 11 introduced the required modifier. If you use it on a field or property, the compiler will ensure that you set the field or property to a value when you instantiate it. 16. When a parameter is passed into a method, it can be passed in one of several ways: By value (this is the default): Think of these as being in-only As an out parameter: Think of these as being out-only.They must be set inside the method By reference as a ref parameter: Think of these as being in-and-out. As an in parameter: Think of these as being a reference parameter that is read-only. 17. Tuples are an efficient way to combine two or more values into a single unit. public (string, int) GetFruit() { return ("Apples", 5); } (string, int) fruit = bob.GetFruit(); WriteLine($"{fruit.Item1}, {fruit.Item2} there are."); You can explicitly specify the field names: public (string Name, int Number) GetNamedFruit() { return (Name: "Apples", Number: 5); } var fruitNamed = bob.GetNamedFruit(); // Deconstruct the return value into two separate variables. (string name, int number) = bob.GetNamedFruit(); // You can then access the separate variables. WriteLine($"{name}, {number}"); 18. Local functions are the method equivalent of local variables. In other words, they are methods that are only accessible from within the containing method in which they have been defined. In other languages, they are sometimes called nested or inner functions. 19. A property is simply a method (or a pair of methods) that acts and looks like a field when you want to get or set a value, but it acts like a method, thereby simplifying the syntax and enabling functionality, like validation and calculation, when you set and get a value. 20. public class ImmutablePerson { public string? FirstName { get; init; } public string? LastName { get; init; } } public record ImmutableVehicle { public int Wheels { get; init; } public string? Color { get; init; } public string? Brand { get; init; } } ImmutableVehicle car = new() { Brand = "Mazda MX-5 RF", Color = "Soul Red Crystal Metallic", Wheels = 4 }; ImmutableVehicle repaintedCar = car with { Color = "Polymetal Grey Metallic" }; Two records with the same property values are considered equal. 21. Stack memory is faster to work with but limited in size. Heap memory is slower but much more plentiful. When you define a type using record struct or struct, you define a value type. This means that the memory for the object itself is allocated to the stack. 22. Publishing a single-file app dotnet publish -r win10-x64 -c Release --no-self-contained /p:PublishSingleFile=true If you prefer the .pdb file to be embedded in the .exe file, embedded 23. from wsl sudo sqlite3 Northwind.db < Northwind4SQLite.sql sqlite3 Northwind.db .tables -- List all tables .schema -- Show all table schemas SELECT COUNT(*) FROM Orders; -- Check if data exists .quit -- Exit 24. The EF Core database providers for SQLite and SQL Server are built on top of the ADO.NET libraries, so EF Core is always inherently slower than ADO.NET. Furthermore, ADO.NET can be used independently for better performance because the EF Core database providers are “closer to the metal.”

No comments:

Post a Comment