开发者

Declarations vs definitions

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 开发者_StackOverflow中文版declaration and where the word definition is used, it is used to mean the same thing as declaration.


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.


  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.


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.


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")


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜