C#使用system.io而不是在我的课堂上工作,但在main中工作

bad*_*986 1 .net c# class using-directives

我正在研究一个我以前不记得的问题.我正在使用VS2012 C#

当我添加使用System.IO; 我的主程序一切正常,但是当我将它添加到我的类文件时,它不会让我使用所有的方法.

using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FoxySearch
{    
    class FoxySearch
    {
         File.    <<<<----- here i want to add File.Exists("Blablalba")
    }
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它不会让我添加它.一旦我添加了intellisense关闭的时间段并且没有显示任何选项.当我自己键入它时它显示为红色并且说,

System.IO.File.Exists(string) 是一种方法,但像一个类型一样使用

Jon*_*eet 6

你还没有给出足够的代码来肯定地说,但听起来你可能试图直接在类声明中编写"普通代码",而不是在方法或属性声明中.

类只能包含声明 - 方法声明,字段声明等.你不能写:

class Foo
{
    int i = 10; 
    Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)

第一行是有效的,因为它是一个变量声明 - 第二行不是,因为它只是一个方法调用.如果您将代码移动到一个方法,那么它没关系:

class Foo
{
    public void Bar()
    {
        int i = 10; 
        Console.WriteLine(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,我建议你重新审视你的命名 - 对一个类和名称空间使用相同的名称是个坏主意.