Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Use field or property inside the same class

Tags:

c#

When referring to a value inside a class (from within the same class), should you use the field or the property that can be accessed from other classes?

For example, which way should I be referring to a variable in my class, and why?

public static class Debug
{
    private static int _NumberOfEvents = 1;

    public static int NumberOfEvents
    {
    get
    {
        return _NumberOfEvents;
    }
    set
    {
        _NumberOfEvents = value;
    }
}

public static void LogEvent(string Event)
{
    //This way?
    Console.WriteLine("Event {0}: " + Event, _NumberOfEvents);
    _NumberOfEvents++;

    //Or this way?
    Console.WriteLine("Event {0}: " + Event, NumberOfEvents);
    NumberOfEvents++;
}

}

Thanks

like image 802
user9993 Avatar asked Aug 16 '13 02:08

user9993


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

When it is a simple property like this, consider replacing it with an "automatic" property, like this:

public static int NumberOfEvents {get;set;}

With properties this simple, it does not matter which way you access them: although accessing backing variable may seem like a little faster, the optimizer will take care of optimizing out the function call, making both accesses equally fast.

When the property is more complex, for example, when it has additional validations and/or triggers events, the decision becomes more complex: you need to decide if you want to have the effects associated with accessing the property, or if you wish to avoid them. Then you make a decision based on what you want to happen.

like image 171
Sergey Kalinichenko Avatar answered Oct 15 '22 09:10

Sergey Kalinichenko


There is probably no difference in the end, since the JITter will no doubt inline the function call that is generated when using the property.. into a direct field access.

Personally (and in teams I have been in, including the current one), we use the field and leave properties for access outside the class.. unless they are automatic properties (which, surprisingly enough.. happens rarely for us) or they contain logic.

like image 3
Simon Whitehead Avatar answered Oct 15 '22 10:10

Simon Whitehead