简体   繁体   English

Visual Studio Express 2013-创建单独的C#代码文件?

[英]Visual Studio Express 2013 - Create separate c# code file?

The VS 2013 Express forum doesn't seem to exist at Microsoft so I'd like to ask here.. 微软似乎不存在VS 2013 Express论坛,因此我想在这里提问。

I am using Microsoft VS Express 2013 to create a C# project. 我正在使用Microsoft VS Express 2013创建C#项目。 I'd like to be able to add a whatever.cs file to the project so that I can put extra functions there instead of in the default Program.cs file. 我希望能够向项目中添加一个what.cs文件,以便可以在其中放置其他功能,而不是将其放在默认的Program.cs文件中。 Back in the old days, we could import code files in C by using a #include but C# in the Visual Studio doesn't seem to do this. 在过去,我们可以使用#include导入C中的代码文件,但Visual Studio中的C#似乎不这样做。

I have been able to successfully add a .cs file, create a class within it, and then instantiate the class and call it's methods from within Program.cs but I'd rather not have to instantiate a variable and have to call functions like something.MyFunction() just to execute some code that exists in another file. 我已经能够成功添加.cs文件,在其中创建一个类,然后实例化该类并从Program.cs内调用它的方法,但我宁愿不必实例化变量也不必像调用某些东西一样调用函数.MyFunction()仅用于执行另一个文件中存在的某些代码。

Is this even possible? 这有可能吗? If not, does anybody know why? 如果没有,有人知道为什么吗? I always like the #include in C. You could keep things nice and neat. 我一直很喜欢C中的#include。您可以让事情保持整洁。

Files added to a Visual C# project are automatically "included" in every other file within that namespace. 添加到Visual C#项目的文件会自动 “包含”在该命名空间内的所有其他文件中。 You do not need a using statement unless you change the namespace. 除非您更改命名空间你不需要 using语句。 Because of this, there is no equivalent of the "#include" directive from C/C++. 因此,没有C / C ++的“ #include”指令的等效项。

Now to handle your use case. 现在处理您的用例。 C# is inherently object-oriented. C#本质上是面向对象的。 It is not expected that you create a million functions and call them individually (like you do in C). 预计您不会创建一百万个函数并分别调用它们(就像在C中一样)。 So, if you want to use multiple files (and you should!) you have a few options: 因此,如果您想使用多个文件(应该!),则可以选择以下几种方法:

  1. Create a normal class (as you have already done) and instantiate it to call its methods. 创建一个普通的类(已经完成)并实例化它以调用其方法。 This is the preferred method, and you should be able to come up with plenty of classes for your program that make sense. 这是首选的方法,您应该能够为您的程序提供很多有意义的类。

  2. Create a static class. 创建一个static类。 These don't have to be instantiated (you access them like MyStaticClass.MyFunc(); ). 这些不必实例化(您可以像MyStaticClass.MyFunc();一样访问它们)。 These are often used as "helper" classes. 这些通常用作“帮助”类。 In general, use sparingly as they are hard to unit test/dependency inject. 通常,请谨慎使用,因为它们很难进行单元测试/依赖性注入。

  3. Mark your class as partial . 你的类的partial This allows you to define the same class over multiple .cs files. 这使您可以在多个.cs文件中定义相同的类。 Again, this should be used sparingly (see Jon Skeet's answer: https://stackoverflow.com/a/2895068/1783619 ) 同样,应谨慎使用它(请参阅Jon Skeet的答案: https : //stackoverflow.com/a/2895068/1783619

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM