Declarations vs definitions

后端 未结 6 2312
名媛妹妹
名媛妹妹 2021-02-19 18:10

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 met
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 18:44

    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.

提交回复
热议问题