我正在研究一个我不记得以前遇到过的问题。
我正在使用 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")
}
}
由于某种原因,它不会让我添加它。一旦我添加了句点,智能感知就会关闭并且没有显示任何选项。当我自己输入它时,它会显示为红色并说,
System.IO.File.Exists(string)
是一种方法,但用作类型
最佳答案
您还没有真正给出足够的代码来确定,但听起来您可能正在尝试直接在类声明中编写“普通代码”,而不是在方法或属性声明中。
类只能包含声明——方法声明、字段声明等。你不能这样写:
class Foo
{
int i = 10;
Console.WriteLine(i);
}
等等。第一行是有效的,因为它是一个变量声明 - 第二行不是,因为它只是一个方法调用。如果将代码移动到方法中,那就没问题了:
class Foo
{
public void Bar()
{
int i = 10;
Console.WriteLine(i);
}
}
此外,我建议您重新命名 - 对类使用相同的名称,命名空间是 bad idea .
关于使用 system.io 的 C# 在我的类(class)中不起作用,但在 main 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947845/