Showing posts with label C# Programming. Show all posts
Showing posts with label C# Programming. Show all posts

28/02/2025

Question on C# version on VS Projects

I have recently inherited a Visual Studio 2022 project, it is a C# Console application on .Net Framework 4.8. So I thought not a very old project what can go wrong.

Well, when I worked on the project, only into first few hours, I have faced following error:

Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.

I got this error because I tried to use Nullable type in my new code I was developing. But I was thinking, why would this project is using C# 7.3? At the time of writing latest version of the C# was version 13, which was released with .NET 9.0 in 2024. So how come C# version used in relatively new project has taken version 7.3?

To find the answer I have turned to ChatGPT and web. This is what it had to tell:

The default version of the C# depends on the the target framework of your project. Here is the default versions for .NET Framework 4.6.2 and 4.8:

 .NET Framework Version

 Default C# Version

Maximum Supported Version

 .Net Framework 4.6.2

 C# 7.0

 C# 7.3

 .Net Framework 4.8

 C# 7.3

 Latest version as of now


That explained lot. This project was created on .Net Framework 4.8, so it has defaulted to C# version 7.3.

Next question pop-up to me was, then how can I upgrade C# version on this project to more latest version, so I can use new features in the language in my code?

Answer is, you need to manually edit the .csproj file (i.e. Visual Studio project file). You need to insert something similar to below:

<PropertyGroup>
  <LangVersion>8.0</LangVersion>
</PropertyGroup>

If you want to use latest version of the language forever, you can do following:
<PropertyGroup>
  <LangVersion>latest</LangVersion>
</PropertyGroup>

However one of the thing to consider is, although .Net Framework 4.8 support latest version of the language, some of the features require runtime support which is only available in Framework. For example async streams, interface methods require runtime support of .Net Core/.NET 5+.

Unfortunately, there is no setting to tell on Visual Studio project to tell, which version of C# language in use. So if your csproj file doesn't have above property, you need to infer that your project will use the default version of the Framework as the language version.

If you are using more modern Frameworks, here are the default C# versions go with them:
  • .NET 8 => default to C# 12
  • .NET 7 => default to C# 11
  • .NET 6 => default to C# 10

30/11/2024

Using Codestral in VS Code - Part 1

This is a part of the series of article where I explain how I learning AI code assistance in VS Code.

You can read the first article here. It explains how you connect Codestral to VS Code.

Ok, now we have connected Codestral to VS Code, lets see how we can use it to assist my coding. In this part I'm going to ask Codestral to write a whole program for me and probably small edit. To simplicity I'm going to use .Net Core console application and ask Codestral to create a snake game in C#.

Get project created

First let's get project created for our game in VS Code. Open a folder and create a new folder called "SnakeGageCodestral" (or any preferred name).

Then open the terminal in VS Code and create console project by running following command:

dotnet new console

That will create the basic structure with Program.cs file in it.


Open the Program.cs and clear any code in it.


Get AI Assistance

Open "Continue" extension. Select your AI modal (in this case we are using Codestral) and ask the question from AI.

You can use a prompt like this: Can you create a snake game in .Net Core console project in C sharp?



Codestral will be more helpful than you think and will also include steps to create project also, but we are intrested in code section. Choose that section (step 4 in below example) and apply it.


This will get updated in Program.cs file.
Check for any obvious compile time errors and if there are you can get assistance from AI by just putting the error message in chat box.

Mine was ok and next step is to run it. Use following command on VS Code terminal.

dotnet run

Mine worked ok.



Editing using AI

Though it worked first time. Snake was running too fast. I wanted to slow it down. So I asked AI assistance to make a edit. It has suggested the line that I should edit as per screen shot below.


That's it. We will have look at more features later.

16/11/2023

Passing dynamic parameter to Base Constructor

 While working on a C# program this week, came across interesting scenario where I needed to pass parameter to base constructor of a class. Unfortunately, base constructor is developed by a third party, so we don't really couldn't change it.

Early days when we use this base class, we use to hard code the parameter to it in inherited class constructor. This week we had a new requirement, where that parameter required to be dynamic or read from configuration file.

To demonstrate it with example, let's assume my base class is ISOCar (where it build ISO standard car). I inherit this class and create my own modified car - let's call it MyBrandCar. Let's also assume base class has one constructor which accept engine capacity. So my code will be some like below:

public class MyBrandCar : ISOCar

{

    public MyBrandCar() : base(1.5)

    {

    }

}

My new requirement is, I need to read the engine capacity from a config file or calculate it from some other values from a config file.

So I was wondering what is the best way to approach this. Because constructor is called first and base constructor is called even before inherited class constructor, there was no way to fetch the value from the config file.

Little bit of Google suggested me to use static method. So I changed my code to following:

public class MyBrandCar : ISOCar

{

    public static decimal GetEngineCapacity()

    {

        // read the config and return the value

    }


    public MyBrandCar() : base(GetEngineCapacity())

    {

    }

}

Since static methods are executed even before constructors, this has worked.

Simple, but elegant way,

Using Own API keys in Various IDEs

AI hype is so high these days, every one want best AI models for least cost. Though they don't cost much individually, when you add up c...