简体   繁体   English

C#基本理解问题

[英]C# Basic Comprehension Questions

I am ashamed that this is a most basic task to accomplish in C# and I can't seem to figure it out. 我很this愧这是用C#完成的最基本的任务,我似乎无法弄清楚。 Yeah, I can compile it and make it run, but I want to dissect it and be understand it, word for word. 是的,我可以编译它并使其运行,但是我想逐字逐句地剖析并理解它。

using System;  *//"include standard System classes"*
namespace DataTypeApplication  *//"Create new classes within DataTypeApp..."*
{
   class Program  *//"Name this class "Program""*
   {
      static void Main(string[] args)  *//Declares the main function of this class?*
      {
          Console.WriteLine("Size of int: {0}", sizeof(int));  *//Print string using sizeof(int)*
          Console.ReadLine();  *//Irrelevant*
      }
   }
}

I am confused as to why this will run. 我对为什么会运行感到困惑。 Program has been created but not called. 程序已创建但未调用。 And also, this outputs 4 for the int, but where the heck does the 4 come from? 而且,这为int输出4,但是4到底来自哪里呢?

I'd appreciate any help understanding this, although, maybe C# just isn't for me. 我会很感激您的理解,尽管也许C#不适合我。 -_- -_-

Program has been created but not called 程序已创建但未调用

Program.Main is your program's entry point, which is baked into the metadata of you .exe file. Program.Main是程序的入口点,它被烘焙到.exe文件的元数据中。 You can see the entry point under your project settings in Visual Studio, or if you use ILDASM to de-compose your file you'll see it in the header section. 您可以在Visual Studio中的项目设置下看到入口点,或者如果使用ILDASM分解文件,则会在标题部分看到它。

And also, this outputs 4 for the int, but where the heck does the 4 come from 而且,这会为int输出4,但是4到底在哪里呢?

The sizeof operator will yield the size in bytes of the unmanaged type. sizeof运算符将产生非托管类型的大小(以字节为单位)。 The int keyword is an alias for Int32 , which is a 4 byte representative of an integral type. int关键字是Int32的别名,它是4字节,代表整数类型。

Every program has its entry point - usually this entry called Main function in your case: 每个程序都有其入口点-通常在您的情况下,此入口称为Main函数:

void Main(string[] args)

sizeof(int) returns you 4 since this is the size of int type on your machine - 4 bytes. sizeof(int)返回4因为这是计算机上int类型的大小4个字节。

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

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