Csharp

C# Dev Kit

C# Dev Kit is a Visual Studio Code extension made by Microsoft that adds full C# support to the editor. Without it, VS Code has no understanding of C# — with it, you get the tools you need to write, run, and debug C# programs.

What it adds#

Installing C# Dev Kit gives VS Code three things it doesn’t have by default for C#:

Intelligent autocomplete — as you type, VS Code suggests completions based on the C# language and your project’s code. It shows available methods, properties, and variables so you don’t have to remember everything.

Class (C#)

A class in C# is a blueprint for creating objects, defined with the class keyword.

This page covers Class in C#. For a language-agnostic introduction, see Class.

Defining a Class#

public class Player
{
}

public is the access modifier — it means the class can be used from anywhere in the program. The class body sits between the curly braces.

By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding Fields and Properties#

A field stores data on the object. A property provides controlled access to that data.

Comment (C#)

This page covers Comment in C#. For a language-agnostic introduction, see Comment.

A comment in C# is text the compiler ignores. C# has two comment syntaxes: single-line and block.

Single-line comment#

A single-line comment starts with // and continues to the end of the line.

// Everything on this line is a comment.
Console.WriteLine("Hello, World!");

You can also write it inline, to the right of code:

Console.WriteLine("Hello, World!"); // Everything to the right is a comment.

Block comment#

A block comment starts with /* and ends with */. Everything between those markers is ignored, regardless of how many lines it spans.

Constructor (C#)

A constructor in C# is a special method inside a class that runs automatically when you create an object with new.

This page covers Constructor in C#. For a language-agnostic introduction, see Constructor.

Defining a Constructor#

A constructor has the same name as the class and no return type — not even void.

public class Player
{
    public string Name;

    public Player(string name)
    {
        Name = name;
    }
}

Name = name stores the argument on the object so it’s available later.

Create a C# Project in Visual Studio Code

This procedure walks you through creating a new C# console project in Visual Studio Code using the C# Dev Kit extension.

Prerequisites#

Steps#

  1. Open Visual Studio Code.
  2. Open the Explorer panel by clicking the file icon in the left sidebar.
  3. Click Open Folder and choose a folder for your project, or create a new one.
  4. Open the Command Palette with Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac).
  5. Type .NET: New Project and select it from the list.
  6. Select Console App from the project templates.
  7. Enter a name for your project and press Enter.
  8. Confirm the folder where the project will be created and press Enter.
  9. When prompted for the solution file format, select .sln and press Enter.

Result#

Your project folder will contain:

Method (C#)

A method in C# is a function defined inside a class that defines what an object can do.

This page covers Method in C#. For a language-agnostic introduction, see Method.

Defining a Method#

A C# method has an access modifier, a return type, a name, and a body.

public void Greet()
{
    Console.WriteLine("Hello!");
}
  • public — the access modifier; public means the method can be called from anywhere
  • void — the return type; void means the method returns nothing
  • Greet — the method name; use PascalCase by convention
  • () — the parameter list; empty here, but arguments go inside these parentheses

Return Types#

If a method produces a value, declare the return type instead of void and use return to send it back.

Property (C#)

A property in C# is a member of a class that provides controlled access to a value. It looks like a variable when you use it, but it’s backed by get and set accessors that control how the value is read and written.

Auto-Property#

The simplest form is the auto-property. The compiler generates the backing storage automatically.

public class Player
{
    public int WinCount { get; set; }
}
  • get — allows the value to be read
  • set — allows the value to be written

Using it looks exactly like using a variable:

Run a C# Project in Visual Studio Code

This procedure walks you through running a C# console program in Visual Studio Code using the terminal.

Prerequisites#

Steps#

  1. Open Visual Studio Code with your project folder open.
  2. Open the built-in terminal with Ctrl+\`` (Windows/Linux) or Cmd+`` (Mac). You can also go to View → Terminal.
  3. Make sure the terminal is in your project folder — you should see the folder name in the terminal prompt. If not, navigate to it with cd folder-name.
  4. Save any unsaved changes with Ctrl+S (Windows/Linux) or Cmd+S (Mac).
  5. Run the program with:
dotnet run

Result#

The program builds and runs. Its output appears in the terminal directly below the dotnet run command.