C# Track

Story, Bites, and Repositories

Story Index Introduction Summary of language and peek at what is coming Models Models of structure and semantics Data Types & instances Operations Callable objects Structures Class structure Libraries Standard and community libraries References Resource links    
Bite Index Introduction Getting started Hello Hello world program Data Type system and instances Objects Code abstractions Generics Writing flexible code Iteration Using std lib and user-defined enumerable containers Execution Details of managed execution model libraries Programming facilities Glossary Glossary of CSharp terms    
Figure 1. C# demo code
"C# is designed to be a platform-neutral language, a workhorse for cross-platform applications."
- Anders Hejlsberg, creator of C#

C# is a general-purpose programming language with a bias toward using Windows APIs and Azure services. It has well developed facilities for web programming and building graphical user interfaces. The C# compiler, CLR virtual machine, and dotnet command line interface have been ported from Windows to Linux and macOs platforms and can generate code for those platforms using the same techniques used for Windows. Using the MAUI framework, C# is used to build cross-platform mobile applications using the same code base for Android, iOS, and Windows. There are five ways of viewing CSharp content in this site:
  1. CSharp Story
    An ebook with seven chapters that covers CSharp programming at intermediate level.
  2. CSharp Bites
    A collection of pages each focused on one feature of the CSharp programming language, starting with basics.
  3. CSharp Repositories
    A collection of code for components, projects, and demonstrations.
  4. Blogs
    There are now, as of 07 Jan 2025, no C# blogs. That is expected to change eventually.
  5. dotnet fiddle Playground code examples
It's easy to sample each of these views by using the links above, or more selectively, using links in the C# Explorer's panel on the left (if you don't see that click on the "toggle ctrl panel" button at lower left).
What I like about CSharp
  • Ability to build high quality user interfaces
    • Windows Presentation Foundation (WPF)
      • Defines UI structure with eXtensible Application Markup Language (XAML)
      • Supports routed events: Direct Events handle events at the event source, Event Bubbling events travel up the XAML dependency tree until finding an element with an appropriate handler, and Tunneling Events that travel from the XAML root down to the event source to let other elements on that path to start the event handling.
    • Asp.Net Framework
      • Supports building web sites that manage user-supplied data with Views and Data Models. Views are populated with data from the local data model and presented to the user or accept structured data from the user to mutate local data model.
      • The framework natively supports the Model View Controller site structure and can interact with both local and cloud supported data models.
    • MAUI
      • Enables cross-platform development of mobile applications for Android, iOS, and Windows. A single code base coupled with the MAUI framework supports an application for all three platforms.
  • Safety
    • Strict type safety
      • C# enforces strict type checking at compile time
      • Eliminates many run-time errors, but null reference checking is optional using nullable reference types.
    • Memory safety
      • C# uses garbage collection to safely automate allocation. That comes at the expense of lower performance and latency issues at startup and randomly during collection operations.
    • Thread safety
      • The C# language provides a lock keyword for providing exclusive access to a critical section.
      • The .Net library provides Monitor, Mutex, ReaderWriterLock, Semaphore, SpinLock, EventWaitHandle, and System.Threading.Lock constructs. It does not provide a condition variable, but that can be crafted using Monitor.Wait, Monitor.Pulse, and Monitor.PulseAll.
      • The compiler does not check for data sharing between threads without locking. That means that there are no data race safety guarantees.
    • Unsafe blocks
      • C# supports unsafe blocks in which safety checks are suspended. Code has to opt-in to unsafe code by configuring each project .csproj with <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  • Excellent code resources
    • dotnetfiddle playground web app
    • .Net tools
      • Visual Studio 2022 Integrated Development Environment
        • Project and Solution construction
        • MSBuild code builder
        • Execution engine
        • Project types for Asp.Net and Xamarin
      • Visual Studio Code Editor
        • light weight IDE for Windows, Linux, macOS
        • Project and Solution construction
        • MSBuild code builder
        • Execution engine
        • Project types for Web and MAUI apps using dotnet CLI in terminal window
      • Other tools
        • dotnet CLI - build and execute from command line
        • dotnet format - applies rules supplied in .editorconfig settings
        • DocFX - generates documentation from XML comments
        • StyleCop - enforces style and consistency rules for C#
    • std library
      • standard libraries are accessible for all the .Net platforms including Asp.Net and Xamarin.
      • The Base Class Library includes facilities for collections, file I/O, networking, string handling, and exception handling.
      • .Net provides a standard library interface specification that applies to all .Net platforms including Windows, Linux, and macOS.
      • System.Collections provides: ArrayList, Hashtable, Queue, Stack, SortedList, and BitArray.
      • System.Collections.Generic provides: List<T>, Dictionary<Tkey, TValue>, Queue<T>, Stack<T>, SortedList<TKey, TValue>, SortedDictionary<TKey, TValue>, SortedSet<T>, HashSet<T>, LinkedList<T>, ReadOnlyColletion<T>, KeyValuePair<TKey, TValue>
      • System.Collections.Concurrent provides: ConcurrentDictionary<Tkey, TValue>, ConcurrentQueue<T>, ConcurrentStack<T>, ConcurrentBag<T>, BlockingCollection<T>
    • .Net Community Open Source libraries
    • Code Examples
      • Coming soon
  • Excellent learning materials
    • Coming Soon
Try exercises at the bottom of this page
The code above on the right illustrates a C# Hello World program, often encountered when learning a new language.

Table 1. C# Resources from this site

Site Resources Content
C# Repositories Index of all the CSharp code repositories
C# Models Summary of features with screenshots and examples - pdf
C# Story
Index at upper right on this page.
CSharp ebook in 7 chapters
first chapter in C# Story
C# Bites
Index at upper right on this page.
Code Bites about C# features and patterns
C# Glossary Definitions of common terms
C# FlashCards Basic types and data structures
dotnetfiddle Playground online compiler
Code Examples Content
C# Repositories Index into C# Repositories
CSharp Code examples
Other Resources Content
C# Guide Definition and examples of CSharp collections, iterators, and concurrency constructs.
Tooling Using Visual Studio Code to create and build C# code
C# home site Download and install,
C# user's forum Create an account and you can log-in, read, and post messages and questions.
The C# Reference Semi-formal reference for C#
dotnet API Guidelines Guidelines for crafting C# code
 

Exercises

  1. Write a CSharp program to find the largest file in a specified directory tree, using walkdir crate from crates.io.
  2. Ammend Project #1 by accepting arguments from the command line, using args crate.
  3. Use ChatGPT to create a directory traversal crate and use that instead of walkdir.
  4. Write a CSharp program to read a source code file and count the number of lines and number of scopes for each function. You can count scopes by counting open braces "{". Start by searhing for fn and then read each line searching for "{" and "}". Push each open brace on a stack, and pop when a closed brace is encountered. When the stack is empty, the end of the function has been reached.
  5. Modify Project #4 by counting lines and scopes for each file in a directory tree rooted at a specified folder, usually named "src".