In C# how does a declaration differ from a definition, i.e.:
Answers to original questions 1, 2, 3: no difference in C#
However might be worth mentioning those terms in regards to methods:
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.
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.
int x; // 'x' is declared
x = 10; // 'x' is assigned
int y = 20; // 'y' is both declared and assigned
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.
It doesn't because C# doesn't have to make a distinction.
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.
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.