使用system.io的C#在我的班上没有醒来,但可以在main中使用 [英] C# using system.io not woking in my class but works in main
问题描述
我正在处理一个我以前从未记得过的问题。
我正在使用VS2012 C#
I am working on an issue I do not remember ever having before. I am using VS2012 C#
当我使用 System.IO
添加时;在我的主程序中,一切正常,但是当我将其添加到类文件中时,它不会让我使用所有方法。
When i add using System.IO
; to my main program everything works fine, however when I add it to my class file it will not let me use all of the methods.
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")
}
}
由于某种原因,它不会让我添加。加上句号后,智能感知将立即关闭并且不显示任何选项。当我自己输入时,它显示为红色,并说:
For some reason it wont let me add it. As soon as I add the period the intellisense closes and shows no options.When I then type it out myself it shows red and says,
System .IO.File.Exists(string)
是一种方法,但是像类型一样使用
System.IO.File.Exists(string)
is a method but is used like a type
推荐答案
您确实没有给出足够的代码来肯定地说,但这听起来像是您可能正在尝试直接在类声明中而不是在方法或属性声明中编写普通代码。
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.
这篇关于使用system.io的C#在我的班上没有醒来,但可以在main中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!