2

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?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Selman Genç
  • 100,147
  • 13
  • 119
  • 184

4 Answers4

2

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.

killermist
  • 105
  • 5
mathk
  • 7,973
  • 6
  • 45
  • 74
2

From the C# programming guide page on statements,

The actions that a program takes are expressed in statements. Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition. The order in which statements are executed in a program is called the flow of control or flow of execution. The flow of control may vary every time that a program is run, depending on how the program reacts to input that it receives at run time.

A statement is something that tells the language to make an action.

A class member declaration does not itself define an action; it defines some property of an object.

This is backed up by drilling down through the C# grammar starting from statement; everything under statement is arguably an action or an executable line of code.

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • :) I hat when language have arguably grammar-English-term-definition. It is very hard to define the terms. – mathk Dec 22 '14 at 14:43
1

what is the correct term for a member declaration?

Class member declarations

class-member-declaration:
  constant-declaration
  field-declaration
  method-declaration
  property-declaration
  event-declaration
  indexer-declaration
  operator-declaration
  constructor-declaration
  destructor-declaration
  static-constructor-declaration
  type-declaration

The problem is the documentation you linked to is half plain English/half BNF. I find this browsable pure BNF more useful.

weston
  • 54,145
  • 21
  • 145
  • 203
0

I believe you want to look at section 10 - classes. For example, a member function is defined in 10.5 - Methods: http://msdn.microsoft.com/en-us/library/aa645760(v=vs.71).aspx.

On edit: (added 'for example' to first line), also:

Simple declaration statements do not cover the (potential) necessary additional information to define a member of class (or struct, etc, as mathk points out). A declaration statement, as you point out, is defined as:

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

But when you look at the definition of local-variable-declaration, for example, as:

local-variable-declaration:
    type local-variable-declarators

you can see why member declarations are special - extra information such as modifiers needs to be covered. Basically, the 'type' part of that definition has to already be resolved to an earlier definition (or built-in type). For more complex definitions (such as members), the grammar definition requires additional attributes covered in other sections (10,11,etc) that do not apply to the basic declaration-statement case. Once the underlying type is defined, declaring an instance of that particular "kind-of-thing" is then a declaration-statement.

frasnian
  • 1,973
  • 1
  • 14
  • 24
  • I don't see why `member` should be limited to methods. – mathk Dec 22 '14 at 14:00
  • It's not, for some reason I thought OP was asking about member functions as opposed to members in general. Was already in process of updating my answer :) – frasnian Dec 22 '14 at 14:02
  • Now I have to ask you why do you think that `member` is limited to `class member` ? struct member exist too. :) – mathk Dec 22 '14 at 14:04
  • I'm not looking for this. I'm asking _are member declaration some kind of statement? if they are, what kind?_ – Selman Genç Dec 22 '14 at 14:10