Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarations vs definitions

Tags:

c#

.net

In C# how does a declaration differ from a definition, i.e.:

  1. A class declaration vs a class definition
  2. A variable declaration vs definition
  3. A method parameter declaration vs definition

In C++, this was rather obvious, but in C# from what I can tell from the ECMA standard and MSDN is that everything is a declaration and where the word definition is used, it is used to mean the same thing as declaration.

like image 854
Michael Goldshteyn Avatar asked Oct 31 '10 19:10

Michael Goldshteyn


2 Answers

where the word definition is used, it is used to mean the same thing as declaration

Correct.

The concept of a 'declaration' as a soft/forward definition is needed in C and C++ because of their compilation model. C++ (conceptually) uses single pass compilation, C# is multi-pass. Consider:

class Bar; // declaration: needed in C++, illegal and unnecessary in C#

class Foo  // start of definition, counts as a declaration of Foo
{
    Foo f; // use of declared but still incompletely defined class Foo
    Bar b; // use of declared but still undefined class Bar
}

class Bar  //  definition and re-declaration  
{
}

C++ can't handle the Bar b field without a declaration first. C# can.

like image 195
Henk Holterman Avatar answered Oct 29 '22 05:10

Henk Holterman


Answers to original questions 1, 2, 3: no difference in C#

However might be worth mentioning those terms in regards to methods:

  • Declaration is place in Interface where we just "declare" the signature of the method
  • Definition is the actual place where we "define" the actual implementation of the declared previously method. (same as "implementation")
like image 26
Dmytro Sokhach Avatar answered Oct 29 '22 05:10

Dmytro Sokhach