莱文·史密斯的面试问题 [英] interview question at levin soft

查看:52
本文介绍了莱文·史密斯的面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友

hi friends

does main() method has public access modifier always?

推荐答案

入口点(通常为Main)不应公开.实际上,它最好是私有的,并且总是由Visual Studio创建为私有的 .访问修饰符未编写,默认情况下是公共的.

通过显式添加访问修饰符"private",很容易检查到这一事实,它确实起作用.

现在,我想解释为什么解释我的Manas是错误的:Main可以被Framework完美地访问.实际上,访问说明符不会阻止Reflection来访问访问类型和成员.使用BindingFlags可以很容易地进行检查.

—SA
Entry point (usually Main) should not be public. Actually, it should better be private and it is always created by Visual Studio as private. The access modifier is not written, it is public by default.

This fact is easy to check up by adding access modifier "private" explicitly — it does work.

Now, I want to explain why explanation my Manas is wrong: Main can perfectly be accessed by the Framework. In fact, access specifiers do not prevent Reflection to access types and members. This is easy to check up using BindingFlags.

—SA


我们做一个小实验,

第1步:编写一个包含以下内容的小程序,

Let''s do a small experiment,

Step 1: Write a small program with following contents,

namespace MainMethodTester
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main Method Tester");
        }
    }
}



步骤2:构建解决方案.

第3步:打开.Net Reflector程序(我假设您有该程序,否则, .Net Reflector [< ^ ])并拖放.Net Reflector程序旁边的已构建exe文件(来自debug文件夹).

步骤4:然后从.Net Reflector窗口打开Main Method,您将看到Main方法的以下代码,



Step 2: Build the solution.

Step 3: Open .Net Reflector program (I assume you have the program or otherwise, .Net Reflector[^]) and drag and drop the builded exe file(from the debug folder) in side the .Net Reflector program.

Step 4: Then Open the Main Method from the .Net Reflector window and you will see following code for the Main method,

private static void Main(string[] args)
{
    Console.WriteLine("Main Method Tester");
}



在默认情况下Main是私有的.

希望对您有所帮助:)



in where Main is private BY DEFAULT.

Hope it helps :)


是的. Main必须具有public access修饰符,原因是Main被视为应用程序的入口点,它可以在一个程序集中甚至在不同的程序集中调用,因此另一个程序集如何才能调用非公共的Main方法.

有关MSDN的更多信息:

http://msdn.microsoft.com/en-us/library/acy3edy3 (v = vs.80).aspx [
Yes. Main has to have public access modifier.Reason being that Main is considered as the entry point of the application.It can be called within an assembly or even from different assembly.So how can another assembly can call a Main method that is not public.

More on MSDN :

http://msdn.microsoft.com/en-us/library/acy3edy3(v=vs.80).aspx[^]


这篇关于莱文·史密斯的面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆