14

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181

6 Answers6

22

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.

H H
  • 263,252
  • 30
  • 330
  • 514
  • On a related note how does one separate declaration and definition over assemblies? Can declaration be in one assembly and definition in another? – kesarling He-Him Feb 13 '23 at 16:40
11
  1. That distinction does not exist. You are referring to the scope of a declaration, the point of definition is irrelevant to the scope of the variable/field.

  2. int x;  // 'x' is declared
    x = 10; // 'x' is assigned
    
    
    int y = 20; // 'y' is both declared and assigned
    
  3. That doesn't make a lot of sense. A method argument is declared in the method signature.

I think you are a bit confused regarding terminology in 1 and 3.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Well, in C++: int x=10; // Is not a declaration and is strictly a definition. This is why I am confused with C# terminology. Also, C++ allows class declarations which are completely different from class definitions. C# allows partial classes and completely defined classes, but both appear to be declarations! – Michael Goldshteyn Oct 31 '10 at 19:22
  • @michael: No, both are definitions. – H H Oct 31 '10 at 19:26
  • @michael: partial classes are just one class split into multiple source files. Some members are defined in one file and some in others. – Albin Sunnanbo Oct 31 '10 at 19:34
  • @michael, I can't understand what is declaration and what is definition? I think in c++ int x = 10; is definition and declaration. would you explain it? – Saeed Amiri Oct 31 '10 at 19:35
  • @Henk, no, they are declarations, see Section 10.1 (for a complete class) and sections (10.2.4 and 10.2.7) of the C# 4.0 Standard document for partial classes. – Michael Goldshteyn Oct 31 '10 at 19:37
  • @Michael: You're probably right, I was using the C++ naming. The correction: They are both _like_ C++ definitions. – H H Oct 31 '10 at 19:54
  • 4
    Let me suggest that it is silly to read the C++ spec in order to try and understand C# semantics. Just read the C# spec! =) – Ed S. Oct 31 '10 at 20:07
  • After `int x;` , `x` is very well defined. It just isn't assigned. – H H Nov 07 '10 at 09:39
11

The C++ build model dates from an era where ferrite cores were a dollar a dozen. Multiple source code files compiled one at a time. With a single-pass compiler. A linker to glue everything together. That made separate declarations in header files and prototypes necessary.

The C# compiler takes ready advantage of modern machine resources. All source code files that constitute an assembly are compiled at the same time, the compiler makes at least two passes so it can parse declarations before method bodies. There's still a possible link model but it gets very rarely used.

The only notions of a declaration having to match a definition that I can think of is a delegate type having to match a target method and an interface member declaration having to match its concrete implementation.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
4

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")
Dmytro Sokhach
  • 171
  • 1
  • 3
2

It doesn't because C# doesn't have to make a distinction.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
1

According to Kernighan & Ritchie in "The C Programming Language": A "declaration" announces the properties of variables; it consists of a name and a list of variables, such as: int fahr, celsius;

According to Stroustrup in "The C++ Programming Language": A "declaration" is a statement that introduces a name into the program. It specifies a type for that name. A type defines the proper use of a name or an expression.

Neither book specifically defines "definition". But both use the term in the scientific sense of the VALUE of a variable. Thus a function declaration declares the function's calling signature. A function definition contains the actual code.

The need for having separate meanings in these languages is because of yesteryear's compilers. They needed to know the types of names ahead of time, before the name was actually used. Otherwise they would need to make another pass through the source code.

John Pankowicz
  • 4,203
  • 2
  • 29
  • 47