简体   繁体   English

在与C#中的类不同的文件中包含Main()例程

[英]Having a Main() routine in a separate file from a Class in C#

I recently branched out into C# written with Visual Studio Express from C++ written with gVIM. 最近,我从使用gVIM编写的C ++扩展到使用Visual Studio Express编写的C#。 I've been told I'll have to unlearn a lot of stuff to really use C# effectively, but here's my question: 有人告诉我,我必须学习很多知识才能真正有效地使用C#,但这是我的问题:

In C++ when writing a class or data type, I would have separate files for the class definition and the driver program. 在C ++中,编写类或数据类型时,我将为类定义和驱动程序提供单独的文件。 I would then #include the class in the driver program in order to use it. 然后,我将#include包含在驱动程序中以便使用它。 It isn't terribly clear how to do this in Visual Studio Express 2013 and all the tutorials I've looked up have the class definition and the Main() routine in the same file. 目前还不清楚如何在Visual Studio Express 2013中执行此操作,并且我查找的所有教程都在同一文件中包含类定义和Main()例程。

I currently have only two files in my solution folder: the driver program p1.cs and the type definition/implementation targetInt.cs . 我的解决方案文件夹中目前只有两个文件:驱动程序p1.cs和类型definition / implementation targetInt.cs What is the best way to allow p1.cs to work with my targetInt.cs type? 允许p1.cstargetInt.cs类型一起使用的最佳方法是什么? Will it simply have access by virtue of being part of the same solution? 它是否会由于成为同一解决方案的一部分而简单地获得访问权限? If so, how to I get around not having a Main() routine in my type definition? 如果是这样,我该如何避免类型定义中没有Main()例程?

Here is a screenshot of the solution and the error I'm getting when I try to build the solution. 这是解决方案的屏幕快照,以及尝试构建解决方案时遇到的错误。 I don't get an error for trying to declare a targetInt object in p1.cs which points to the namespace already being shared. 尝试在p1.cs中声明一个targetInt对象(指向已共享的命名空间)时,我没有得到任何错误。

http://i793.photobucket.com/albums/yy218/tombombodil/solution_zps6a743e2d.png http://i793.photobucket.com/albums/yy218/tombombodil/solution_zps6a743e2d.png

Let me know if I need to clarify anything. 让我知道是否需要澄清任何事情。

The problem as you've written boils down to the simple lack of shared namespaces - because targetInit exists in a separate namespace, Program needs a using targetInit.cs to access the targetInit type. 您所写的问题归结为缺少共享名称空间的简单原因-因为targetInit存在于单独的名称空间中,所以Program需要using targetInit.cs来访问targetInit类型。 They can, however, access each other by virtue of being in the same project - a Solution can contain multiple Projects, and if they don't reference each other, they can't access each other's types. 但是,它们可以由于在同一个项目中而彼此访问-一个解决方案可以包含多个项目,并且如果它们彼此不引用,则它们将无法访问彼此的类型。

Usually, the naemspace of any given class is actually the folder path to it, and the class name is the same as the file name (which Visual Studio does for you when you make new class files). 通常,任何给定类的naemspace实际上是它的文件夹路径,并且类名与文件名相同(在创建新的类文件时Visual Studio会为您提供)。

As for the Main() definition, you only want one of these since you only have a single entry point for the system to jump to when your program begins - having multiple Main() functions doesn't make much sense when the OS needs a clear place to begin execution. 至于Main()定义,您只需要其中之一,因为在程序开始时,系统只有一个入口可以跳转到该入口-在操作系统需要一个Main()函数时,具有多个Main()函数没有多大意义。明确的地方开始执行。

The Main() method and class definitions sitting in the same file is a convenience so all the code can be read together - to get an idea for how actual projects are set up, trying going to GitHub and forking a couple of open-source projects. 位于同一文件中的Main()方法和类定义很方便,因此所有代码都可以一起读取-了解如何设置实际项目,尝试转到GitHub并创建两个开源项目。

It's really not terribly complicated, but it is different from C++. 确实不是很复杂,但是它与C ++不同。 So if you have one file that looks something like this: 因此,如果您有一个看起来像这样的文件:

namespace MyNamespace
{
    public class MyClass
    {
        //...stuff
    }
}

And then you want another file with your Main (which you will for anything more than a trivially simple project), it would look something like this: 然后,您想要带有Main另一个文件(除了一个简单的项目之外,您还将执行其他操作),它看起来像这样:

using MyNamespace;   // unless you use the same namespace for both

namespace SomeOtherNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            var c = new MyClass();
            // alternatively, without the using statement, you can just fully qualify 
            // your class name like so:
            // var c = new MyNamespace.MyClass();
        }
    }
}

But do note that the files need to be in the same project . 但是请注意,文件必须位于同一项目中 If they are in different projects you can still do it, but you have to add a reference to the project with MyClass to the project with Program . 如果它们在不同的项目中,您仍然可以执行此操作,但是您必须将对MyClass的引用添加到Program What you can't do is just have an orphaned C# file floating around in your solution and expect it to work. 您不能做的就是在解决方案中浮动一个孤立的C#文件,并期望它能正常工作。

there are two approaches to use another class in C# directly: 有两种方法可以直接在C#中使用另一个类:
1-putting that class in the same namespace of my class(even if they were in separate files), this code clarify this: 1将那个类放在与我的类相同的名称空间中(即使它们位于单独的文件中),此代码也澄清了这一点:

//file TargetInt.cs
namespace MyNameSpace
{
    class TargetInt
    {
    }
}  
//file p1.cs
namespace MyNameSpace
{
    class p1
    {
        static void Main(string[] args)
        {
        }
    }
}

notice that both classes are in MyNameSpace namespace. 请注意,这两个类都在MyNameSpace命名空间中。
2-if the other class is contained within another namespace, you can simply use it by declaring this statement in the upper beginning of the file: 2-如果另一个类包含在另一个名称空间中,则可以通过在文件的开头声明以下语句来简单地使用它:

//file TargetInt.cs
namespace OtherNameSpace
{
    class TargetInt
    {
    }
}
//file p1.cs
using OtherNameSpace;
namespace MyNameSpace
{
    class p1
    {
        static void Main(string[] args)
        {
        }
    }
}

with using OtherNameSpace; using OtherNameSpace; you can use TargetInt class directly. 您可以直接使用TargetInt类。

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

相关问题 我如何访问从单独的类到主类的getter和setter(在C#中向下转换) - How do I get access to getters and setters from a separate class to the main class(with downcasting in C#) 在C#中使用来自单独类的控件 - Using control from a separate class in C# 如何从单独的 class 中提取字符串,同时将 class 保留在我的主列表中(C#,Unity) - How can I pull a string from a separate class while keeping the class in a list in my main (C#, Unity) 用c#开发一个文件读取例程 - Develop a file reading routine in c# 从静态例程C#访问方法 - Accessing a method from a static routine c# 来自单独的类文件的ASP.NET(C#)中的异常处理 - Exception Handling in ASP.NET (C#) from a separate class file 从C#程序调用自定义R例程(在.R文件中)的步骤是什么? - What are the steps for calling a custom R routine (within a .R file) from a C# program? 运行一个使用主线程中的方法的单独线程是否仍然在主线程上运行它? (C#) - Will running a separate thread that uses a method from the main thread still run it on the main thread? (C#) c#将事件上的参数从另一个类传递到主类 - c# Passing parameters on event from another class to main class 针对更改事件编写C#类的验证例程 - Write a validation routine for a C# class on change event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM