As far as I know C#
allows you to create classes inside other classes. Why am I getting this error and how it can be fixed? Error: Type or namespace definition, or end-of-file expected
using System;
public class Program
{
public static void Main()
{
class Person {
public string Name{ get; set; }
};
Person person = new Person();
}
}
CodePudding user response:
A class can be defined in another class, but not inside a method.
public class Program
{
public static void Main()
{
Person person = new Person();
}
class Person
{
public string Name { get; set; }
};
}
CodePudding user response:
You can create a class within some other class. Method cannot contain a class declaration. However you can access other class within a method. You can correct your code as follows;
using System;
class Program
{
static void Main(string[] args)
{
//Accessing Person Class in Main Method
Person person = new Person();
}
//Class Person within class Program
class Person
{
public string Name { get; set; }
};
}
You can learn more about Nested Classes
from Here