Post

Getting started with DOTNET

Introduction

The official .NET Landing Page The official .NET Landing Page

.NET is a free, cross-platform, open source developer platform for building many different types of applications. With .NET, you can use multiple languages, editors, and libraries to build for web, mobile, desktop, gaming, and IoT.

  • I assume that you already have VS Code installed on your machine
  • And you have a basic understanding of how to use it

Download and install the .NET SDK

  • To begin using .NET, you will need to install the .NET SDK (Software Development Kit).
  • The SDK includes the runtime, libraries, and tools you need
    • to create,
    • build,
    • test,
    • run,
    • and publish .NET applications.

Starting a new project with .NET in Visual Studio Code

Creating a new console application using the .NET CLI

  • Open a new terminal in VS Code
  • Run the following command to create a new console application
1
2
dotnet new console -o <project-name>
# Example: dotnet new console -o MyConsoleApp
  • This command creates a new console application project in the specified directory with the given name

Creating a new console application using the VS Code GUI

Creating a new console application using the VS Code GUIt Creating a new console application using the VS Code GUI

  • Open the command palette by pressing Ctrl + Shift + P
  • Type > .NET: Create a new project
  • Select Console Application from the list of available project types
  • Choose the location where you want to save your project
  • Type the name of your project and press Enter
  • Open the newly created project folder and navigate to the Program.cs file
  • You can now start writing your C# code
  • To run the application, open the terminal and type dotnet run

Terminal Greetings Example

  • In this example, we will create a simple console application that greets the user based on the current date and time.
  • Open the Program.cs file in your project folder
  • Replace the existing code with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Console.WriteLine("What is your name?");
var name = Console.ReadLine();
var currentDate = DateTime.Now;
var currentTime = currentDate.Hour;
var message = $"It's {currentDate:t} on {currentDate:d}!";

if (currentTime < 12)
{
    Console.WriteLine($"Good morning, {name}! {message}");
}
else if (currentTime < 18)
{
    Console.WriteLine($"Good afternoon, {name}! {message}");
}
else
{
    Console.WriteLine($"Good evening, {name}! {message}");
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
  • depending on the time of day, the application will greet the user with “Good morning,” “Good afternoon,” or “Good evening.”
  • Run the application by typing dotnet run in the terminal
  • Enter your name when prompted and press Enter
  • The application will display the appropriate greeting based on the current time as shown below:
1
2
3
4
What is your name?
Ndimofor
Good evening, Ndimofor! It's 6:45 PM on 8/15/2024!
Press any key to exit...
This post is licensed under CC BY 4.0 by the author.