What would be the difference between fields and variables in C#?

Asked

Viewed 321 times

2

What would be the difference between fields and variables in C#?

  • Field is a variable of any type.

  • Field is usually the name used for private variables of class scope and variable is usually used to refer to local scope variables within a method for example.

1 answer

3


I’ve shown you before what a variable.

The field is a member of a data structure, i.e., of a type such as struct or class that contains a state, ie a value. This value can be constant or variable. So field is a specific way to have a variable.

Then the field will be present in types, in case the static fields, or will be in instances (you have you asked what it is).

It makes no difference whether the field visibility is private, protected, internal or public.

If you want to know about the difference between the local variable and the instance variable (the question is not that, but in any case...), already was answered.

Some people confuse attribute with field. Programming languages have field and no attribute, at least in this context. Attribute is another for languages. Unfortunately UML uses this term for field, and people misunderstand and there mix concepts in different contexts, write articles, answers and even books in the wrong way.

On the other hand maybe I wanted to know about the Difference between "Attribute" and "Instance Variable".

It should also be of interest: What’s the difference in instantiating, initializing and declaring a variable?. You might also want to know Public field X property. And yet Difference between "Attribute" and "Instance Variable". As I said before, search the site and follow links will find various contents very good to learn these concepts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.