Search code examples
c#declarationterminologystatements

Is class member declaration not a statement?


I thought the class member declarations are declaration statements. But when I look at the declaration statements from the spec. I found this:

declaration-statement:
      local-variable-declaration;
      local-constant-declaration;

Obviously a class member declaration neither local variable nor local constant. So, what is the correct term for a class member declaration? There is no separate chapter for class member declarations in the Statements section, so are they not statements? if they are which category do they belong to?


Solution

  • declaration statement can be seen as a subset of declaration. Eventually declaration statement is the intersection of statement and declaration.

    Now concerning members, you have to know which kind of member you are dealing with. For class you have class member declaration etc...

    By reading your comment I think you are getting confused with the term statement. In a language, a statement should be a computable thing. C# does not strictly follow these rules (many other language also do not). To enhance the declaration statement.

    For example:

    int i;
    int i = 0;
    

    Both are a declaration statement. The computational part is the allocation in the heap space and/or the initialization of the value. To simplify a declaration is something that will be use in the lexical scope analyzer and/or the linker. But, a statement is something mainly used in the bytecode emitter

    Going back to member declaration. You could argue that a member declaration can embed computation and be correct. For example:

    class a { static int i = 42;}
    

    Alas again C# is not strictly following the definition and should have named this a class-member-declaration-statement. But the name becomes silly and not all statements are accepted in class member declarations.