Colton Colcleasure
Colton Colcleasure

Reputation: 518

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

Upvotes: 2

Views: 121

Answers (2)

Pavel Durov
Pavel Durov

Reputation: 1297

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.

Upvotes: 2

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149618

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.

Upvotes: 4

Related Questions