为什么我需要包装类? C#

发布于 2024-07-30 01:52:31 字数 796 浏览 5 评论 0 原文

我有一个相当长的程序,有一个类和许多方法。 所有这些都必须包含在一个巨大的类中,该类包装了除 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。

哦,呃,区分大小写。 谢谢。 但仍然-> 为什么我在不使用测试类时需要包装类?

I have a program of some length, with a class and many methods. All of these have to be contained in one huge class that wraps the entire file except the using statements. The necessity of that huge wrapping class seems pointless, is this used only later when making a program with multiple files?

    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?
    {}

    }

this is my full file. i compile csc file.cs then file.exe.

oh duh, case sensitive. thanks. but still -> why do i need the wrapping class when nott using the test class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水晶透心 2024-08-06 01:52:31

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 be public 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 to public 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)

み零 2024-08-06 01:52:31

在 C# 中应该是 Main <---大写 M :D

In C# it should be Main <---capital M :D

清风无影 2024-08-06 01:52:31

在我看来,当您应该使用 命名空间

除此之外...在 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.

甜心 2024-08-06 01:52:31

Main 必须具有正确的签名才能被识别为程序启动方法。 这意味着每个班级只能拥有 1 个。 每个程序可以有多个,您可以在编译器命令行或项目|属性|应用程序中选择应提供 main 的类(查找组合框“启动对象”)。

那个大类听起来很奇怪,你能概括一下你的意思吗? 猜猜看,您是否在这里混淆了 classnamespace

添加

为了响应您的编辑,在 C# 中,每个方法(包括 Main)都必须位于一个类中。 请参阅此发布。 根据定义,main 的签名是

public static void Main(string[] args) { }

尽管您可以省略 args 参数。 您的情况下的命令行将是:

csc /main:CollectKeywordsFromLogs  file.c

当您省略 /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 and namespace 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

public static void Main(string[] args) { }

Although you are allowed to omit the args parameter. The command line in your case would be:

csc /main:CollectKeywordsFromLogs  file.c

There are default rule rules when you leave out /main

万人眼中万个我 2024-08-06 01:52:31

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) ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文