❔ 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:
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
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
Thats correct, but I simply cannot select a higher version than 4.8. Let me take a screenshot of it.
you picked the wrong project type when you created it
$newproject
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
yeah
You should never select anything with
.NET Framework
in the nameyes
yup, pick the 6.0 one
no not that one
The top one
Yes i know, just wanted to show you haha
That one question remains; why it has no Main method?
$toplevelcode
"top level statements"
Traditionally in C#, the main entry point of any program would be a static
Main
method (see $mains): 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:
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-statementsTop-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.
It's just slightly cleaner
It makes no practical difference, other than if you really prefer explicitly having the class and method.
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
out of the window?
it simply isnt there anymore
yeah, right
(it technically is still there, you just don't have to write it yourself)
this works so im happy with it haha. Thank you Pobiega and Thinker
correct!
Yeah there's a quick fix to bring it back to the old style thing
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.