开发者

C# Declaration Question

开发者 https://www.devze.com 2023-03-25 07:28 出处:网络
I am very new to c#, but I am learning more every day. I am wondering what the following means: private DataIdentifier dataIdentifier;

I am very new to c#, but I am learning more every day.

I am wondering what the following means:

private DataIdentifier dataIdentifier;

Why do they write like this? It is almost the same name b开发者_如何学Gout they use big D in the first word.


This declares a member variable (i.e. a "field") in a class.

private is the access modifier. It specifies that the variable can only be accessed from within the class. The access modifier is optional, and defaults to private (but it's considered best practice to be explicit anyway).

DataIdentifier is the type of the variable (i.e., only instances of the DataIdentifier class can be put into the variable).

dataIdentifier is the name of the variable. This is what you write in the code when you want to access the object that's being held in the variable.

Note: Technically, objects are not actually "in" a variable; the variable is typically a pointer to a location in memory where the object actually is (it "references" an object).

As you learn more C#, you'll see a common idiom where the name of the property is the same as the name of the type:

public SpaceShip SpaceShip;

This is allowed since the compiler is smart enough to know whether you're referring to the variable or the class itself when you type SpaceShip in your code, just from the context.


DataIndentifier is a type.

dataIdentifier is the name of a field of that type.

The similarity of the names is entirely coincidental.


private DataIdentifier dataIdentifier; 

DataIdentifier is a Type whereas dataIdentifier is a variable declared of that type

Like

private int a;

where int is a type and a is a variable of type Int


This question has been answered several times over, but I would like to instead recommend what I believe to be the BEST introductory book for C#. Even if you don't like learning from books, you'll like this one:

Head First C#

Check out the free chapters and if you like it, buy it. I guarantee you won't be sorry. Hope it helps.

DISCLAMER: I am in no way affiliated to OReilly Media or any of its subsidiaries... ;)


It is just another type of notation. It's really a silly variable name. I wouldn't recommend you follow the same convention. DataIdentifier is the class/object type. private is the access modifier.


The DataIdentifier is the type (class or struct) and dataIdentifer is the name.


DataIdentifier is the type of the variable
dataIdentifier is the name of the variable (it is of the type DataIdentifier)


C# is case sensitive. DataIdentifier and dataIdentifier are two different things.

Here DataIdentifier is the type and dataIdentifier is the variable.


It may be useful for you to relate the example to the actual grammar of C#.

field-declaration:
    attributesopt   field-modifiersopt   type   variable-declarators   ;

field-modifiers:
    field-modifier
    field-modifiers   field-modifier

field-modifier:
    new
    public
    protected
    internal
    private
    static
    readonly
    volatile

variable-declarators:
    variable-declarator
    variable-declarators   ,   variable-declarator

variable-declarator:
    identifier
    identifier   =   variable-initializer

variable-initializer:
    expression
    array-initializer 
0

精彩评论

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