❔ top level statements
i am using .net 7.0 with top level statements. Why does this work:
but not this:
Why does the latter throw an error "Can't resolve symbol 'Multiply'"?
12 Replies
Type declarations have to come after all top-level statements
so anything that is declared outside of the partial class that i have defined is considerd a top level statement? i thought top level statements were the boilerplate code (namespace, class def, main entry method)
Well, all statements above any type declarations
basically i have to call method before define method, correct?
oh hol on
yup, ur right, applies to all statements
C# doesn't have a concept of declaration order
If you define a method
Multiply
then it exists everywhere, not just after the declarationdo you have a link to the docs for that? i can't find anything relating to it online
Thats just how C# works. Why would you need documentation for it?
Its been that way since .NET Framework 1, C# 1
top level statements isnt magic, it just puts all your code inside a
static void Main
method
This isn't python or javascript, its not interpreted line by linegotcha
as a side note: with sharblab.io u can also see what the code looks like that is actually generated from ur code, before it gets compiled:
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFDILIFcA2AXAlgB0wE8AKGAGgAIAWASgG5ll8BDAJzxc0th7gHZkAb2SUxfAGw9qlDDgLESuAHbZKy9AFsADHCoq1GnTFqjxIpOKuUDlNgFMAzljUBedVt2UAVB+ONLazEoOABOEgAiABUAC3s7JxcbRxBKCMowBOccBjMxAF9kfKA==
SharpLab
C#/VB/F# compiler playground.
wow that's pretty cool! thanks for sharing :)
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.