为什么我需要包装类? C#
我有一个相当长的程序,有一个类和许多方法。 所有这些都必须包含在一个巨大的类中,该类包装了除 using 语句之外的整个文件。 那个巨大的包装类的必要性似乎毫无意义,这只是稍后在制作具有多个文件的程序时使用吗?
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
public class CollectKeywordsFromLogs // why do I need this ( when it is the only class [as i just added the test class for the question])
{
//a class
//constructor
//methods
//methods
public static void Main() // and why would i need to explicitly declare this public? it works either way, and a private main would be useless no?
{}
}
这是我的完整文件。 我编译csc file.cs,然后编译file.exe。
哦,呃,区分大小写。 谢谢。 但仍然-> 为什么我在不使用测试类时需要包装类?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C# 中没有全局方法或变量。 它们必须位于类型声明(例如类或结构)内。 这也适用于 Main() 方法。
Eric Lippert 发布了一个很好的答案,为什么 C# 不允许像 C++ 这样的非成员函数,并在 < a href="http://blogs.msdn.com/ericlippert/default.aspx" rel="nofollow noreferrer">他的博客:为什么 C# 不实现“顶级”方法?
可能会发现以下 MSDN 链接很有用:C++ 和 C# 之间的比较
Main() 方法是 C# 程序的入口点。 程序中应该只有一个入口点,但程序中可以有多个包含 Main() 方法的类。 但是,如果您有多个 Main 方法,则应通过使用 编译器选项
/main
。根据 静态,并且不应是
公共
/acy3edy3.aspx" rel="nofollow noreferrer">MSDN。 默认情况下,VS2008 将 Main() 创建为静态私有,运行时可以毫无问题地调用它。 您可以将其更改为public
,它将可以正常编译。 然而,我认为这真的很尴尬,因为你无法阻止其他类调用 Main(),因为它是公共的。有关 Main 方法的更多信息,您可以参阅以下 MSDN 文章: Main()和命令行参数(C# 编程指南)
There are no global methods or variables in C#. They must be inside of a type declaration such as class or struct. This applies to Main() method as well.
Eric Lippert posted a nice answer to SO question, Why C# is not allowing non-member functions like C++, and followed up further in his blog: Why Doesn't C# Implement "Top Level" Methods?
You also may find the following MSDN link useful: Comparison Between C++ and C#
The Main() method is an entry point of C# programs. There should be only one entry point in a program, yet, there can be multiple classes that contains a Main() method in a program. However, if you have multiple Main methods, you should specify which Main method to be used as an entry point by compiling your program with the compiler option
/main
.The Main() method must be
static
and should not bepublic
according to the MSDN. By default, VS2008 creates a Main() as static private and the runtime can call it without any issue. You can change it topublic
and it will compile fine. However, I think it is really awkward since you cannot prevent other classes from calling into Main() as it is public.For more information on Main method, you can see the following MSDN article: Main() and Command-Line Arguments (C# Programming Guide)
在 C# 中应该是 Main <---大写 M :D
In C# it should be Main <---capital M :D
在我看来,当您应该使用 命名空间。
除此之外...在 C# 中,所有代码都必须位于某种类中。 为
Main()
方法设置例外是愚蠢的。顺便说一句,私有
Main()
实际上作为程序入口点可以很好地工作。It seems to me you're using a class, when you should be using a namespace.
Other than that... in C#, all code has to be inside a class of some kind. It would be silly to make an exception for the
Main()
method.A private
Main()
actually works fine as a program entry point, by the way.Main 必须具有正确的签名才能被识别为程序启动方法。 这意味着每个班级只能拥有 1 个。 每个程序可以有多个,您可以在编译器命令行或项目|属性|应用程序中选择应提供 main 的类(查找组合框“启动对象”)。
那个大类听起来很奇怪,你能概括一下你的意思吗? 猜猜看,您是否在这里混淆了
class
和namespace
?添加
为了响应您的编辑,在 C# 中,每个方法(包括 Main)都必须位于一个类中。 请参阅此发布。 根据定义,main 的签名是
尽管您可以省略 args 参数。 您的情况下的命令行将是:
当您省略 /main 时,有默认规则规则
A Main has to have the right signature to be recognized as Program startup method. That means you can have only 1 per class. You can have more than 1 per program, you can select the class that should provide the main either on the compiler command line or in Project|Properties|Application (look for the combobox 'Startup Object').
That big class sounds strange, can you show the outline of what you mean? An to take a guess, aren't you confusing
class
andnamespace
here?Add
In response to your Edit, in C# every method, including Main, must be in a class. See this post. And the signature for main, by definition, is
Although you are allowed to omit the args parameter. The command line in your case would be:
There are default rule rules when you leave out /main
C#使用基于类的面向对象编程范式,全局变量属于过程式编程等其他范式; 请查看此处有关编程范例的文章。 确实,一些面向对象的编程语言(尤其是 C++)支持全局变量,但 C# 不支持(在我看来,这是一件好事 :o) )。
C# uses the class-based object-oriented programming paradigm, global variables belong to other paradigms like procedural programming; have a look here for an article on programming paradigms. True, some object-oriented programming languages, espesially C++, supports global variables, but not C# (and thats a good thiing imo :o) ).