Saturday, December 9, 2023

String to Number conversion

In many scenarios we need to convert our data from one data type to another.

Most important one is converting a text (string) to number (int, float etc).

We can not assign a string data type int (or any number data types) directly.

Let's take the below code as an example

string t = "28";
int age = t;

It will cause the below compilation error

Cannot implicitly convert type 'string' to 'int'

Below are few ways to convert string to text

int age = int.Parse(t);

In case the content of the variable is not in correct format, it will cause a runtime error

The input string '{content}' was not in a correct format.

We can use TryParse method to fix this issue

string t = "28t";
bool isValid = int.TryParse(t, out var age);
if(isValid)
  Console.WriteLine(age);
else
  Console.WriteLine("Invalid data");

Parse and TryParse static methods are available in all numeric data types

Friday, December 8, 2023

VS Code- Recommended Extensions for C#

 Following extensions helps you work with C# in VSCode

C#

Id: ms-dotnettools.csharp
Description Base language support for C#
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp
 

C# Dev Kit

Id: ms-dotnettools.csdevkit
Description: Official C# extension from Microsoft
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit
 

IntelliCode for C# Dev Kit

Id: ms-dotnettools.vscodeintellicode-csharp
Description: AI-assisted development for C# Dev Kit
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.vscodeintellicode-csharp

Sunday, December 3, 2023

Useful dontet CLI commands for projects

To list available project templates

dotnet new list 




To create a solution

dotnet new sln --name HelloWorld

To create a project

dotnet new console --name HelloWorlddotnet new classlib --name HelloWorldLib

To create a project with Program class (Traditional)

dotnet new console --name HelloWorld --use-program-main true

New project commands will create projects without linking them to solution

To link a project to solution

dotnet sln HelloWorld.sln add HelloWorld\HelloWorld.csproj

or if the folder has one solution and the project folder has only one project

dotnet sln add HelloWorld dotnet sln add HelloWorldLib

To remove a project from solution

dotnet sln remove HelloWorld

To build a solution

dotnet build

To build a specific project

dotnet build HelloWorld\HelloWorld.csproj

or

dotnet build HelloWorld

To run the project

dotnet run --project HelloWorld

or if you are in the project folder itself, you don't have to mention the project

dotnet run --project HelloWorld

Saturday, December 2, 2023

Useful dotnet CLI commands

 Below are some of useful dotnet cli commands

List installed SDKs

dotnet --list-sdks

List Installed runtimes

dotnet --list-runtimes

Show Version

dotnet --version

List installed workloads

dotnet workload list

Search workloads available

dotnet workload search

Install a new workload

dotnet workload install <workloadid>

Note: workload id available in workload search

Remove all installed workloads

dotnet workload clean


String to Number conversion

In many scenarios we need to convert our data from one data type to another. Most important one is converting a text (string) to number (in...