0

Using C# as an example. Say there is a class named ExampleClass and the variable is just value. Why would one declare a variable like this

private ExampleClass value;
mshsayem
  • 17,557
  • 11
  • 61
  • 69
Cody Tapp
  • 9
  • 6
  • What is the question? You can declare inside the class space private fields or properties or events. In this case you're declaring a "ExampleClass type" variable with name "value" in "only this class scope" – DrkDeveloper Dec 07 '19 at 16:33

3 Answers3

0

You wouldn't declare a variable named value unless there was some unavoidable requirement to do so.

The reason why is because it is not meaningfully named.

When we name variables meaningfully, our code becomes self-documenting. Well documented code is always worthy goal.

robbpriestley
  • 3,050
  • 2
  • 24
  • 36
0

This is a field and can be declared inside a class. Example:

private class Test
{
    private int _customField;
}

_customField is my field with type int (instead of ExampleClass). People use it to make something more accessible since C# isn't just functions, it's OOP.

misticos
  • 718
  • 5
  • 22
0

There is a thing called Primitive Obsession. Using a class/specific type here is the solution to it.

If you use a class, you got all the Compiler Type checking to help you avoid invalid values.

Take this Enum vs Integer question for examples.

Christopher
  • 9,634
  • 2
  • 17
  • 31