Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How does the compiler find the another part of the partial class?

Tags:

c#

I am using VS2010 SP1.

After creating a demo project for C# Form application, my solution structure has the following information

Form1.cs
   Form1.Designer.cs
   Form1.resx

// File Form1.cs
namespace WinFormApp
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  }
}

// File Form1.Designer.cs
namespace WinFormApp
{
  partial class Form1
  {

    private void InitializeComponent()
    {
      ...
    }
    ...
   }
}

Here is the question, I can see the Form1 is a partial class. However, how does the compiler know where to find the another part of it? *In other words, is there a binding between the file Form1.cs and Form1.Designer.cs?* Here, as you can see, the other part is defined inside Form1.Designer.cs. I assume there are hints out there so that the compiler can quickly find all implementation code for a partial class. Please correct me.

Thank you

like image 923
q0987 Avatar asked May 25 '11 12:05

q0987


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

The C# compiler is a "two pass" compiler. The project system gives the compiler a list of all the files in the project. The compiler first does a "top level" scan of all the files, and builds up a table of which files contain which namespaces, types, methods, properties, events, fields, and so on, but it does not look at method bodies.

When that table is built up we can use it to do "top level" checking of the entire program. We figure out at that time which partial declarations are really "the same class". During this stage we resolve partialness, we resolve base types and constraints on generic parameters, we make sure that all "override" methods actually override a matching virtual method, and so on.

Once the "top level" analysis is done, only then do we analyze the method bodies.

The file names are irrelevant; the compiler only uses them for error reporting. It is perfectly legal to have a dozen classes and a hundred files and a partial declaration of each class in each file. (Though please do not actually do that; it increases the burden tremendously on the IDE when people do stuff like that because it becomes difficult for us to figure out quickly what all the members of a class are while you are typing in changes!)

like image 71
Eric Lippert Avatar answered Sep 19 '22 23:09

Eric Lippert


All files are sent to the compiler, there is no magic here.

like image 39
Otávio Décio Avatar answered Sep 19 '22 23:09

Otávio Décio