C# using system.io not woking in my class but works in main

前端 未结 6 2099
后悔当初
后悔当初 2021-01-16 10:20

I am working on an issue I do not remember ever having before. I am using VS2012 C#

When i add using System.IO; to my main program everything works fine

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 11:09

    You haven't really given enough code to say for sure, but it sounds like you're probably trying to write "normal code" directly in a class declaration, instead of in a method or property declaration.

    Classes can only include declarations - method declarations, field declarations etc. You can't write:

    class Foo
    {
        int i = 10; 
        Console.WriteLine(i);
    }
    

    etc. The first line is valid as it's a variable declaration - the second isn't, as it's just a method call. If you move the code into a method, then it's fine:

    class Foo
    {
        public void Bar()
        {
            int i = 10; 
            Console.WriteLine(i);
        }
    }
    

    Additionally, I'd suggest that you revisit your naming - using the same name for a class and a namespace is a bad idea.

提交回复
热议问题