❔ Choosing correct Console App project.

Hey there, it is a year ago since I touched C#, and now I have to work in it again for a school course. In my solution I need to have 2 projects. 1. A project that only contains LOGIC so i choose for this to be a class library project 2. A project that only contains UI related stuff (it just needs to have a Program.cs) so this is just a console app project For the UI project I chose the Console App .NET Framework variant, but when I want to add a reference in our UI project to the LOGIC project, I get this error:
Severity Code Description Project File Line Suppression State
Warning Project '..\X.LOGIC\X.LOGIC.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.8'. X.UI
Severity Code Description Project File Line Suppression State
Warning Project '..\X.LOGIC\X.LOGIC.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.8'. X.UI
When I add another project, but this time the regular Console App project, and a reference it simply works. However, this Program.cs has no main method as visible in the picture. Is this normal and should I work with this one?
24 Replies
Thinker
Thinker14mo ago
Your issue is that your library targets .NET 6 while your console app targets an outdated version. Your console app should also be targeting .NET 6. not framework
electronic heartbreak.
Thats correct, but I simply cannot select a higher version than 4.8. Let me take a screenshot of it.
Pobiega
Pobiega14mo ago
you picked the wrong project type when you created it $newproject
MODiX
MODiX14mo ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Thinker
Thinker14mo ago
yeah You should never select anything with .NET Framework in the name
Thinker
Thinker14mo ago
yes
Pobiega
Pobiega14mo ago
yup, pick the 6.0 one
Thinker
Thinker14mo ago
no not that one The top one
electronic heartbreak.
Yes i know, just wanted to show you haha That one question remains; why it has no Main method?
Thinker
Thinker14mo ago
$toplevelcode
Pobiega
Pobiega14mo ago
"top level statements"
MODiX
MODiX14mo ago
Traditionally in C#, the main entry point of any program would be a static Main method (see $mains):
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
In C# 9, a feature called top-level statements was added which allows the static Main method to be omitted in favor of brevity and simplicity, and in C# 10, this was made part of the default console application template:
Console.WriteLine("Hello, world!");
Console.WriteLine("Hello, world!");
If you are following a tutorial or example code and your default template looks different, you can still write the exact same code as if you were writing it inside the static Main method. In addition, you can still write static methods inside top-level code. You can however not use top-level code in more than one file. If you wish to use the old traditional entry point, you can still use a Program class with a static Main method. https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
Top-level statements - programs without Main methods
Learn about top-level statements in C# 9 and later. You can create programs without the ceremony of a Program class and a Main method.
Thinker
Thinker14mo ago
It's just slightly cleaner It makes no practical difference, other than if you really prefer explicitly having the class and method.
electronic heartbreak.
Moment please, let me create the console app project haha The reference works as expected. So when you see the pic, the main method is simply said out of the window
Thinker
Thinker14mo ago
out of the window?
electronic heartbreak.
it simply isnt there anymore
Thinker
Thinker14mo ago
yeah, right (it technically is still there, you just don't have to write it yourself)
electronic heartbreak.
this works so im happy with it haha. Thank you Pobiega and Thinker
electronic heartbreak.
correct!
Chiyoko_S
Chiyoko_S14mo ago
Yeah there's a quick fix to bring it back to the old style thing
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts