Everything within a project must be contained in a class. Seen, maybe that’s the problem. Involve your methods, properties and variables all in one class. From: Augusto Henrique
The following example will cause Visual Studio to signal parts of the code as in violation of CS0116. The attempt to compile this code will result in the compilation failing:
// CS0116.cs
namespace x
{
// A namespace can be placed within another namespace
using System;
// These variables trigger the CS0116 error as they are declared outside of a struct or class
public int latitude;
public int longitude;
Coordinate coord;
// Autoproperties also fall under the definition of this rule
public string LocationName { get; set; }
// This method as well: if it isn't in a class or a struct, it's violating CS0116
public void DisplayLatitude()
{
Console.WriteLine($"Lat: {latitude}");
}
public struct Coordinate
{
}
public class CoordinatePrinter
{
public void DisplayLongitude()
{
Console.WriteLine($"Longitude: {longitude}");
}
public void DisplayLocation()
{
Console.WriteLine($"Location: {LocationName}");
}
}
}
Note that in C#, methods and variables must be declared and defined within a class or struct. To learn more about the structure of the program in C#, see the article General structure of a program in C#. To fix this error, rewrite the code so that all methods and fields are contained within a struct or class:
namespace x
{
// A namespace can be placed within another namespace
using System;
// These variables are now placed within a struct, so CS0116 is no longer violated
public struct Coordinate
{
public int Latitude;
public int Longitude;
}
// The methods and fields are now placed within a class, and the compiler is satisfied
public class CoordinatePrinter
{
Coordinate coord;
public string LocationName { get; set; }
public void DisplayLatitude()
{
Console.WriteLine($"Lat: {coord.Latitude}");
}
public void DisplayLongitude()
{
Console.WriteLine($"Longitude: {coord.Longitude}");
}
public void DisplayLocation()
{
Console.WriteLine($"Location: {LocationName}");
}
}
}
About the error:
https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/compiler-messages/cs0116
Similar question:
CS0116 C# A namespace cannot directly contain members, such as fields or methods
This is the complete code?
– Maury Developer
@Maury Developer only the method with problem. I must put the whole code ?
– Levi
No, it was just a question.
– Maury Developer
About the error: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/compiler-messages/cs0116 Similar question: https://answall.com/questions/373527/cs0116-c-um-namespace-não-pode-conter-diretamentos-fields-ou-métod
– Maury Developer
by error vc must have placed the method directly after the namespace, a method must be within a class, check the structure of your code
– Ricardo Pontual
Everything within a project must be contained in a class. Seen, maybe that’s the problem. Involve your methods, properties and variables all in one class. From: Augusto Henrique
– Maury Developer
He really was out of class. Thanks for the time
– Levi