3 minute read

C# CheatSheet is a website storing teaching material for c# development.


C# Environment Setup

Windows:

  • Visual Studio 2019, 2022: .Net 6, support Class Diagram
  • Visual Studio Code: .Net 5, support PUML Class Diagram

MacOS:

  • Visual Studio 2022: not yet support
  • Visual Studio 2019: .Net 5, support Class Diagram
  • Visual Studio Code: .Net 5, .Net 6, support PUML Class Diagram (tested on Mojave)

Linux:

  • Visual Studio Code: .Net 5, support PUML Class Diagram (not yet tested)

Install IDE and .NET Framework

CSharp to PlantUML (For Visual Studio Code Users)

  • CSharp to PlantUML
  • Install
      $ dotnet tool install --global PlantUmlClassDiagramGenerator
    
  • Use
      $ puml-gen ./Program.cs -public
    
  • Copy the text from *.puml and visualize using PUML Viewer

First Program

$ dotnet --info // check the version installed in your system
$ dotnet new console

In Program.cs

using System;

namespace MyBusiness
{
    class Program
    {
        static void Main(string[] args) // Where the application begins
        {
            Console.WriteLine("Hello World!");
            // Equal to System.Console.WriteLine("Hello World!");
        }
    }
}

In MyBusiness.csproj Doc

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

How to Check the .Net Version

$ dotnet --version

How to Build the Program

$ dotnet build

How to Run the Program (Including Build)

$ dotnet run

Namespace

using System;         // System namespace (defined by C#)
                      // The "using" Directive

namespace MyBusiness  // Application namespace (defined by the programmer)
{
    class Program
    {
        // static: shared method of all instances by the class
        // void: return "nothing" in the method
        // string[] args: parameters passed to the main function.
        // The parameters can be taken when lauching the application.
        static void Main(string[] args) // Where the application begins
        {
            Console.WriteLine("Hello World!");  // Console is a system class
        }
    }
}
using static System.Console;  // Console is a static system class
/*
A static class is basically the same as a non-static class,
but there is one difference: a static class cannot be instantiated.
*/

namespace MyBusiness  // Application namespace (defined by the programmer)
{
    class Program
    {
        // static: shared method of all instances by the class
        // void: return "nothing" in the method
        // string[] args: parameters passed to the main function.
        // The parameters can be taken when lauching the application.
        static void Main(string[] args) // Where the application begins
        {
            WriteLine("Hello World!");
        }
    }
}

Selected Theory

Naming Convention Doc

Pascal Case

Use pascal casing (“PascalCasing”) when naming a class, record, or struct. When naming public members of types, such as fields, properties, events, methods, and local functions, use pascal casing. When naming an interface, use pascal casing in addition to prefixing the name with an I. This clearly indicates to consumers that it’s an interface.

Camel Case

Use camel casing (“camelCasing”) when naming private or internal fields, and prefix them with _.

Comments

// This is a single line comment

/*
This is a multi-line comment
and continues until the end
of comment symbol is reached
*/

/*
The following codes show how I often add comments to my programs.
This is a my application namespace, called My Business.
*/
namespace MyBusiness
{
    /*
    The class of my main program
    */
    class Program
    {
        /*
        Main: The class of my main program, where the application begins.
        Input:
          args: input parameters
        Output:
          none
        */
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}

External Resources

Updated: