简体   繁体   中英

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

I'd appreciate any help understanding this, although, maybe C# just isn't for me. -_-

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

And also, this outputs 4 for the int, but where the heck does the 4 come from

The sizeof operator will yield the size in bytes of the unmanaged type. The int keyword is an alias for Int32 , which is a 4 byte representative of an integral type.

Every program has its entry point - usually this entry called Main function in your case:

void Main(string[] args)

sizeof(int) returns you 4 since this is the size of int type on your machine - 4 bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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