tags:

views:

119

answers:

4

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.

+1  A: 

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

Otávio Décio
+5  A: 
  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 Swangren
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
@michael: No, both are definitions.
Henk Holterman
@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
@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?
SaeedAlg
@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
@Michael: You're probably right, I was using the C++ naming. The correction: They are both _like_ C++ definitions.
Henk Holterman
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 Swangren
+3  A: 

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
+1 for ferrite cores.
Euphoric
+5  A: 

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

Correct.

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

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

class Foo
{
    Foo f;  // use of incomplete type, C++ can handle this
    Bar b;  // use of 'undefined' class Bar
}

class Bar  // definition
{
}

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

Henk Holterman