Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference...? [C# properties GET/SET ways]

I know in C# you can easily create accessors to a data type, for example, by doing the following:

public class DCCProbeData
{
    public float _linearActual { get; set; }
    public float _rotaryActual { get; set; }
}

However my colleague, advised me to do it this way:

public class DCCProbeData
{
    private float _linearActual = 0f;

    public float LinearActual
    {
        get { return _linearActual; }
        set { _linearActual = value; }
    }
    private float _rotaryActual = 0f;

    public float RotaryActual
    {
        get { return _rotaryActual; }
        set { _rotaryActual = value; }
    } 
}

My way seems simpler, and more concise. What are the differences and benefits of doing it either way?

Thanks

Edit Just a note, my colleague was able to generate the code for the "second way" using the Refactor option within the Class Details pane most easily found in a Diagram file. This makes it easy to add many Properties without having to manually create the accessors.

like image 261
Ryan R Avatar asked Mar 24 '11 16:03

Ryan R


People also ask

What is difference C and C++?

In a nutshell, the main difference between C and C++ is that C is function-driven procedural language with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.

What is difference in C language?

There is a major difference between C and C++. The C language is a procedural one that provides no support for objects and classes. On the other hand, the C++ language is a combination of object-oriented and procedural programming languages.

Why C++ is better than C differentiate it?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++.

Which is better C or C++?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.


1 Answers

"Your way" just tells the compiler to create the second option. Unless you do something else in the getter or setter, they are functionally identical.

However, with "your way", I would recommend using the proper C# naming conventions. I would personally write this as:

public class DccProbeData
{
    public float LinearActual { get; set; }
    public float RotaryActual { get; set; }
}
like image 99
Reed Copsey Avatar answered Nov 13 '22 16:11

Reed Copsey