C# notes. Page 1.

.NET SDK vs. .NET core
.NET SDK (Software Development Kit) is used to develop applications while .NET Core (.NET runtime) is used to run .NET applications (the runtime environment).

Download and install the .NET SDK (includes the runtime).

Open command-window and run the following to build and run a new C# program..

"c:\program files\dotnet\dotnet" --version
mkdir core
cd core
mkdir newproject
"c:\program files\dotnet\dotnet" new console
cat program.cs
cat core.csproj

"c:\program files\dotnet\dotnet" restore
"c:\program files\dotnet\dotnet" run
"c:\program files\dotnet\dotnet" build
"c:\program files\dotnet\dotnet" run	

To show tree view and include files: tree /F

dotnet restore and dotnet build commands shell out to msbuild. to see the call to msbuild, use the verbose switch:

"c:\program files\dotnet\dotnet" build /v diag

Core functionality needed to create .NET Core project shared between Visual Studio and CLI located at https://github.com/dotnet/sdk
To view entire .NET API catalog: https://apisof.net

   Your application - MyProgram.dll   
   Libraries - AnotherPack.dll   
   .NET Core Runtime   
   .NET Core Dependencies (and libraries)   
   Operating System (win10-x64, ubuntu, osx)   

CLR (Common Language Runtime) sits in memory of a machine and converts IL (Intermediate Language) code to native code. If CLR does not find the correct version, a FileLoadException will be thrown. CLR manages allocation and removal of memory using the Garbage Collector.

Visual Studio creates projects using templates and displays the project with the menu bar, toolbar, and code editor window. Visual Studio will automatically include references to the appropriate assemblies based on the template selected (Console application, Class library, ASP.NET Core Empty, ASP.NET Core Web API, Windows Forms App, Windows Forms Class Library, Unit test project, etc.).

A using directive brings the namespace into scope to avoid using fully-qualified class names in the code. Classes are compiled into assemblies. A class consists of two components: data attributes and methods. Data attributes refer to properties defined in the class object and methods indicate operations that are executed in the class object. The assembly file usually has dll extension, but may also have exe extension. A single assembly may contain multiple namespaces. Conversely, a single namespace may span multiple assemblies.

Variables hold different types of values than can be stored or processed.

   Value types
   bool (true or false) (System.Boolean)
   byte (0 to 255) (System.Byte)
   sbyte (-128 to 127) (System.SByte)
   char (U+0000 to U+FFFF) (System.Char)
   short (-32,768 to 32,767) Signed 16-bit integer (System.Int16)
   ushort (0 to 65,535) Unsigned 16-bit integer (System.UInt16)
   int (-2,147,483,648 to 2,147,483,647) Signed 32-bit integer (System.Int32)
   uint (0 to 4,294,967,295) Unsigned 32-bit integer (System.UInt32)
   long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) Signed 64-bit integer (System.Int64)
   ulong (0 to 18,446,744,073,709,551,615) Unsigned 64-bit integer (System.UInt64)
   float (±1.5 x 10−45 to ±3.4 x 1038) ~6-9 digits, 4 bytes (System.Single)
   double (±5.0 × 10−324 to ±1.7 × 10308) ~15-17 digits, 8 bytes (System.Double)
   decimal (±1.0 x 10-28 to ±7.9228 x 1028) 28-29 digits, 16 bytes (System.Decimal)
   Reference types
   object (System.Object)
   string: sequence of Char objects whose value is immutable/read-only. When a string is modified, it creates a new object in memory. (System.String)
   dynamic: avoids compile-time type checking (System.Object)
   interface: collection of properties, events, methods with declarations, but no definition

Value types vs. Reference types: Value type variables are saved on the stack while reference type variables are saved in the heap (a managed heap data structure). String is an immutable, read-only object. String content cannot be changed after String has been created. Methods that seem to modify a string return a new String object. The "System.Text.StringBuilder" class modifies a string without creating a new object. StringWriter builds a string on StringBuilder and StringReader reads data from StringBuilder.
int x = 40;     // a value type
object o = x;   // boxing saves the value to the heap
int y = (int)o; // unboxing converts number to a value type on the stack
Boxing converts a value type to the type "object" making it a reference type (implicit conversion). Unboxing converts a reference type to a value type (explicit conversion). A reference type points to a memory address. If multiple reference type variables are pointed to the same memory address, an update to one will make the change to the memory address and all will instances of the variable will reflect the updated value.
class BoxingTest
    static void Main()
        int i = 111;
        // Boxing copies the value of i into object o.
        object o = i;
        // Change the value of i.
        i = 222;
        // The change in i doesn't affect the value stored in o.
        System.Console.WriteLine("The value-type value = {0}", i);
        System.Console.WriteLine("The object-type value = {0}", o);
/* Output:
    The value-type value = 222
    The object-type value = 111
A C# method consists of 2 parts: input parameters (variables passed to the method) and return type (value returned by the method when it is finished processing).

A class and class members within a namespace can acquire an access modifier.

   Public: Accessible by all classes in the same assembly as well as classes in different assemblies present in the application.
   Private: Class member accessible in the same class, but not outside the class.
   Protected: Class or class member accessible inside the class or by classes that inherit from the respective class.
   Internal: Accessible by classes in the same assembly but not outside assemblies.
   Protected internal: Accessible by classes in the same assembly or classes present in the outside assembly that inherit from the respective class.
   Private protected: Accessible in the same class or in classes present in the same assembly that inherit from respective class.

Class vs. Struct
Structs are managed as value type variables (on the stack) that will not persist the value into different scopes of the program. Classes are managed as reference type variables (in the heap). Structs do not have default constructors. C# will automatically create a constructor for a class if it does not have one. A class can inherit from other classes, but a struct cannot inherit from another struct. Classes can have default values set in the member variables but structs cannot have default constructors and the application will not assign default values to members of a struct. A struct cannot inherit from other structs or classes, but a class can inherit from other classes.

Inheritance supports code reuse and scalability by providing common member variables across different implementations. If "this" is used, it refers to the attribute in the child class (Employee). If "base" is used, it refers to the attribute in the parent class (Human). All humans are not employees, but all employees are human and can access attributes that are common to all human classes. The example shows the Employee class inheriting attributes from the Human class.
public class Employee : Human
    public Employee()
		this.department = "accounting";
		this.badgeNumber = "123882";
		this.eyeColor = eyeColor;
		this.hairColor = hairColor;
Multiple inheritance is not allowed in C#. To bypass the multiple inheritance restriction, an interface can be used. Another example is a job type of "teaching assistant". A teaching assistant may be a member of faculty (an employee), but may also be a student. C# does not permit multiple inheritance, but Interfaces can simulate multiple inheritance by giving the "TeachingAssistant" access to "Faculty" data members and "Student" data members. All members of an Interface are public.
public class Teaching Assistant : Faculty, IStudent
	public List Courses {get;set;}
	public void PrintGrades()
System.Collections namespace contains interfaces and classes that define various collections of objects.

   Queue (enqueue, dequeue): is first in, first out.
   List, ArrayList: Referenced by index, no key.
   Dictionaries, Sorted Lists, Hashtables: Have a key/value pair.
   Stack (push, pop): is last in, first out.

Use GenericType when possible. Generic types are reference types that can allow for adding/removing objects without need for explicit casting.

   IComparer: Must provide Compare method that compares two objects.
   IEquatable: Requires class to provide Equals method.
   List, Dictionary, Stack, Queue: Provide Contains to compare equality
   ICloneable: .Clone()
   IEnumerable: provides Current, MoveNext, Reset, Dispose

Dispose: The Finalize method is the destructor. To help objects free resources, IDisposable interface (Dispose method) provides destructor. ~Disposable class has a destructor that cannot be called directly. GarbageCollector (GC) calls an objects Finalize method. The GC.SuppressFinalize tells GC to not call object's finalizer. Dispose should free managed and unmanaged resources. Destructors should free unmanaged. Garbage collection can be forced in the system using GC.Collect();.

Using a nuget package "Microsoft.Web.WebView2" to display a web browser and allow a user to go to a specific URL.
private async void btnGo1_ClickAsync(object sender, EventArgs e)
  await web1.EnsureCoreWebView2Async();
  web1.CoreWebView2.Settings.IsStatusBarEnabled = false;
  web1.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
  web1.Source = new Uri(txtAddress1.Text);

Copyright Oproot Research. All rights reserved.
Permission is granted for limited, non-commercial use of text and images. If used, please credit and notify Oproot Research. If circumstances permit, please include the URL: http://oproot.com. Oproot Research would appreciate a copy of publication. High-resolution images are also available. Please email requests, comments to tech@oproot.com.



  P.O. Box 235
  Hondo TX 78861
  email: code@oproot.com