C
C#2y ago
j_uice

❔ Ignoring Main?

just starting out with c# and have a pretty basic question. Trying to just make a random array of ints and print them out.
class sorting {

public static void Main(string[] args) {
int[] numsToSort = getListOfNums();
for (int i = 0; i < numsToSort.Length; ++i) {
Console.WriteLine(numsToSort[i]);
}


}

public static int[] getListOfNums() {
Random rand = new Random();
int[] numsToSort = new int[100];
for (int i = 0; i < numsToSort.Length; ++i) {
numsToSort[i] = rand.Next(100);
}

return numsToSort;

}

}
class sorting {

public static void Main(string[] args) {
int[] numsToSort = getListOfNums();
for (int i = 0; i < numsToSort.Length; ++i) {
Console.WriteLine(numsToSort[i]);
}


}

public static int[] getListOfNums() {
Random rand = new Random();
int[] numsToSort = new int[100];
for (int i = 0; i < numsToSort.Length; ++i) {
numsToSort[i] = rand.Next(100);
}

return numsToSort;

}

}
output:
sorting.cs(1,7): warning CS8981: The type name 'sorting' only contains lower-cased ascii characters. Such names may become reserved for the language. [C
:DivideAndConquer.csproj]
\sorting.cs(2,20): warning CS7022: The entry point of the program is global code; ignoring 'sorting.Main(string[])' entry point. [\DivideAndConquer.csproj]
System.Int32[]
sorting.cs(1,7): warning CS8981: The type name 'sorting' only contains lower-cased ascii characters. Such names may become reserved for the language. [C
:DivideAndConquer.csproj]
\sorting.cs(2,20): warning CS7022: The entry point of the program is global code; ignoring 'sorting.Main(string[])' entry point. [\DivideAndConquer.csproj]
System.Int32[]
53 Replies
Angius
Angius2y ago
First time I see anything like that... I guess it offers some strict checks Like, in C# the naming convention is usually PascalCase, but your class has a lowercase names And, yeah, lowercase names are usually reserved for the language constructs like string, int, bool, etc The second one... could it be about your code missing a namespace?
j_uice
j_uiceOP2y ago
oh yes, if I change to class Sorting then it gets rid of one error but still warning CS7022: The entry point of the program is global code; ignoring 'Sorting.Main(string[])' entry point. let me try
MODiX
MODiX2y ago
Angius#1586
The second one... could it be about your code missing a namespace?
Quoted by
<@!85903769203642368> from #Ignoring Main? (click here)
React with ❌ to remove this embed.
j_uice
j_uiceOP2y ago
namespace Name {
class Sorting {

public static void Main(string[] args) {
int[] numsToSort = getListOfNums();
for (int i = 0; i < numsToSort.Length; ++i) {
Console.WriteLine(numsToSort[i]);
}


}

public static int[] getListOfNums() {
Random rand = new Random();
int[] numsToSort = new int[100];
for (int i = 0; i < numsToSort.Length; ++i) {
numsToSort[i] = rand.Next(100);
}

return numsToSort;

}

}
}
namespace Name {
class Sorting {

public static void Main(string[] args) {
int[] numsToSort = getListOfNums();
for (int i = 0; i < numsToSort.Length; ++i) {
Console.WriteLine(numsToSort[i]);
}


}

public static int[] getListOfNums() {
Random rand = new Random();
int[] numsToSort = new int[100];
for (int i = 0; i < numsToSort.Length; ++i) {
numsToSort[i] = rand.Next(100);
}

return numsToSort;

}

}
}
warning CS7022: The entry point of the program is global code; ignoring 'Sorting.Main(string[])' entry point.
System.Int32[]
warning CS7022: The entry point of the program is global code; ignoring 'Sorting.Main(string[])' entry point.
System.Int32[]
jayfromengland
Visual Studio supports writing code without a main or classes. Could do that if you're just looking to play around.
j_uice
j_uiceOP2y ago
Oh I would like a main and classes. Just trying to get this setup before getting more into it moving over from Java, but just not understanding what the issue is here
Omnissiah
Omnissiah2y ago
is this all of the code? or is there something above what you are showing
j_uice
j_uiceOP2y ago
this is all of it
Angius
Angius2y ago
No other file in the project? Besides the .csproj of course
jayfromengland
I'm struggling to reproduce that with just the code you shared.
j_uice
j_uiceOP2y ago
two empty .cs files
Angius
Angius2y ago
Is one named Program.cs by chance?
j_uice
j_uiceOP2y ago
no
Angius
Angius2y ago
huh
jayfromengland
What are they named?
j_uice
j_uiceOP2y ago
jayfromengland
Ahh you're in Visual Studio Code rather than Visual Studio
j_uice
j_uiceOP2y ago
yes I just went to dotnetfiddle (online ide) and it runs like I want it to
Angius
Angius2y ago
How did you create this project? dotnet new console?
j_uice
j_uiceOP2y ago
yes
Omnissiah
Omnissiah2y ago
not even a using System?
Angius
Angius2y ago
No need Global usings since .NET 6
j_uice
j_uiceOP2y ago
even with that it doesn't work
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Angius
Angius2y ago
Uhhhhh... A long shot, but rename the Sorting class to Program, and the sorting.cs file to Program.cs...? It shouldn't matter, since only the Main matters for the entry point, but who knows
j_uice
j_uiceOP2y ago
same thing so this seems like it might be a vs code thing?
Angius
Angius2y ago
Shouldn't be
Omnissiah
Omnissiah2y ago
put an explicit entry point in the csproj... like <StartupObject>Name.Sorting.Main</StartupObject> (if i remember correctly)
jayfromengland
When you run dotnet build is the warning displayed under "Build succeeded"?
Angius
Angius2y ago
Or just yeet the main and use top-level statements lol
j_uice
j_uiceOP2y ago
warning CS7022: The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.
jayfromengland
Weeeeird
Angius
Angius2y ago
Aight, time to experiment
int[] numsToSort = GetListOfNums();
for (int i = 0; i < numsToSort.Length; ++i) {
Console.WriteLine(numsToSort[i]);
}

static int[] GetListOfNums() {
Random rand = new Random();
int[] numsToSort = new int[100];
for (int i = 0; i < numsToSort.Length; ++i) {
numsToSort[i] = rand.Next(100);
}

return numsToSort;
}
int[] numsToSort = GetListOfNums();
for (int i = 0; i < numsToSort.Length; ++i) {
Console.WriteLine(numsToSort[i]);
}

static int[] GetListOfNums() {
Random rand = new Random();
int[] numsToSort = new int[100];
for (int i = 0; i < numsToSort.Length; ++i) {
numsToSort[i] = rand.Next(100);
}

return numsToSort;
}
this is as proper of a code as it can be, with as few moving parts as it can have No namespace, no class, no main If that doesn't work, I'm at a loss
j_uice
j_uiceOP2y ago
error CS8802: Only one compilation unit can have top-level statements. cursed pc
Angius
Angius2y ago
Delete all the other files you have that are empty
j_uice
j_uiceOP2y ago
omg
Angius
Angius2y ago
Leave just Program.cs with this code
j_uice
j_uiceOP2y ago
j_uice
j_uiceOP2y ago
it was that tempCodeRunnerFile.cs i didnt make that
jayfromengland
Oh, so deleting that fixed it? I was about to come onto that :p
j_uice
j_uiceOP2y ago
thank yall 🙂
jayfromengland
Woohoo What was in it?
Angius
Angius2y ago
If the file was completely empty, chances are the compiler saw it as a top-level statements entry point
j_uice
j_uiceOP2y ago
its an extension I have on vscode I guess it does that for some reason
int[] numsToSort = new int[100];
Console.WriteLine(numsToSort);
int[] numsToSort = new int[100];
Console.WriteLine(numsToSort);
jayfromengland
Even if I try adding that file, I can't reproduce it
j_uice
j_uiceOP2y ago
I am not sure why 😄
jayfromengland
Oooh Woohoo! Adding that code reproduces it. That makes sense You have one file with classes and one that's just standalone code. Feels weird to me to have it like that.
j_uice
j_uiceOP2y ago
thank yall for the help, I was starting to go crazy lol
jayfromengland
It doesn't know whether to run the Main or the standalone code No worries
j_uice
j_uiceOP2y ago
Indirectly I learned about having just top level statements so that is nice
Angius
Angius2y ago
TLS are great
jayfromengland
I didn't know they were called "top-level statements" so I learned a thing.
Accord
Accord2y 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