How to develop a C# application in Visual studio code

C# development in Visual studio code

To develop a C# application in Visual Studio Code, you will need to have the following installed:

  1. Visual Studio Code: You can download the latest version of Visual Studio Code from the official website.

  2. .NET Core SDK: You can download the latest version of the .NET Core SDK from the official website.

  3. C# extension for Visual Studio Code: This extension can be installed directly from within Visual Studio Code by searching for "C#" in the Extensions marketplace.

Getting started with C#

Here are some basic C# instructions that are commonly used in C# programming:

  1. Variables: Variables are used to store data in C#. The basic syntax for declaring a variable is <data type> <variable name> = <value>;.

     For example:

      int myNum = 5;               // Integer (whole number)
      double myDoubleNum = 5.99D;  // Floating point number
      char myLetter = 'D';         // Character
      bool myBool = true;          // Boolean
      string myText = "Hello";     // String
    
  2. Data Types: C# supports various data types such as int, double, float, char, string, etc.

  3. Conditional statements: Conditional statements are used to make decisions in a program. The basic syntax for an if statement is if (<condition>) { <code to execute if condition is true> }.

  4. Loops: Loops are used to execute a block of code multiple times. The basic syntax for a for loop is for(int i=0; i<5; i++) { <code to execute> }

  5. Functions: Functions are used to group a set of instructions together. The basic syntax for a function is <access modifier> <return type> <function name> (<parameters>) { <code to execute> }.

  6. Classes: Classes are used to define the blueprint for an object. The basic syntax for a class is <access modifier> class <class name> { <class members> }

  7. Arrays: Arrays are used to store a collection of similar data types. The basic syntax for an array is <data type>[] <array name> = new <data type>[<size of array>];

  8. Namespaces: Namespaces are used to organize code and to prevent naming conflicts. The basic syntax for a namespace is namespace <namespace name> { <code> }

  9. Switch: the switch statement is used to control the flow of a program based on the value of an expression. The expression is typically a variable, and the value of the variable is used to determine which case block within the switch statement should be executed. 

    string name = "John";string name = "John";
    switch (name)
    {
        case "John":
            Console.WriteLine("Hello John");
            break;
        case "Mary":
            Console.WriteLine("Hello Mary");
            break;
        case "Chris":
            Console.WriteLine("Hello Chris");
            break;
        default:
            Console.WriteLine("Hello unknown person");
            break;
    }
    

 Simple C# console application

In this example, the program first prompts the user to enter the base length and height of a triangle. The input is then stored in the baseLength and height variables as a double using the Convert.ToDouble() method.

Then, the program calculates the area of the triangle using the formula (base * height) / 2 and stores the result in the area variable.

Finally, the program prints the area of the triangle to the console using the Console.WriteLine() method.

You can run this program by opening it in Visual Studio Code and running it using the "dotnet run" command from the terminal.

When you run this program it will ask you to enter the base and height of a triangle, and then it will give you the area of that triangle.

C# example


using System;

class Program {
    static void Main(string[] args) {
        double baseLength, height;
        Console.WriteLine("Enter the base length of the triangle:");
        baseLength = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Enter the height of the triangle:");
        height = Convert.ToDouble(Console.ReadLine());
        double area = (baseLength * height) / 2;
        Console.WriteLine("The area of the triangle is: " + area);
    }
}

Object oriented object in C#

In C#, object-oriented programming is implemented using classes and objects. A class is a blueprint for an object, and an object is an instance of a class. Classes define the properties (data) and methods (functions) that an object can have, and objects are created from these classes.

For example, you could create a class called "Car" that has properties like "make," "model," and "year," and methods like "start," "stop," and "drive." You could then create multiple objects from this class, such as a "toyota" object and a "honda" object, each with their own specific values for the properties.

In C#, classes are defined using the "class" keyword and objects are created using the "new" keyword. Here is an example of a simple class and object in C#:


class Car {
    public string make;
    public string model;
    public int year;

    public void Start() {
        Console.WriteLine("Car started.");
    }

    public void Stop() {
        Console.WriteLine("Car stopped.");
    }

    public void Drive() {
        Console.WriteLine("Car is driving.");
    }
}

// Create an object of the Car class
Car toyota = new Car();
toyota.make = "Toyota";
toyota.model = "Camry";
toyota.year = 2020;

toyota.Start();
toyota.Drive();
toyota.Stop();


Visual studio code

Checking the dotnet version

dotnet --version
# 7.0.102 (or whatever version yours is)
Create a new solution sln.
dotnet new sln
Create a new project and open it in VSCode
dotnet new console -o app
cd app
code . # to open project in VSCode
dotnet sln add app.csproj # add project to the solution
Build your code:
dotnet build
To execute your code:
dotnet run

 C# for Visual studio code


 Other dotnet commands:

 dotnet clean #Clean:
 dotnet new #New
 dotnet pack  #Pack
 dotnet publish  #Publish
 dotnet restore  #Restore
 dotnet watch run # Watch

Enjoy C# !

Documentation

Thank you for attention !

For questions please contact me on email simedruflorin@automatic-house.ro

Have nice day !  

Back to top of page

Etichete

Afișați mai multe

Arhiva

Afișați mai multe