|
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.. Commands: "c:\program files\dotnet\dotnet" --version mkdir core cd core mkdir newproject "c:\program files\dotnet\dotnet" new console powershell ls cat program.cs cat core.csproj exit "c:\program files\dotnet\dotnet" restore "c:\program files\dotnet\dotnet" run tree "c:\program files\dotnet\dotnet" build "c:\program files\dotnet\dotnet" runOutput: ![]() 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
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 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 stackBoxing 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.
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 ListSystem.Collections namespace contains interfaces and classes that define various collections of objects.
Use GenericType when possible.
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();. Misc 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.   |    |
   |
![]()   Oproot   P.O. Box 235   Hondo TX 78861   email: code@oproot.com |