Question about variable decleration

This maybe is a stupid question.

Sometimes I see variables declared like

int _Counter;

What is the _ stands for can someone give me an example why you have to declare it like that.

Thank you.



Answer this question

Question about variable decleration

  • Bitfrost

    you are welcome , but i just want to know "Meen zalamak"

    best regards



  • JPA_OVNI

    I have a code that uses that, so I tought that it was used for a special purpose.

    public partial class Scene1
    {
    private System.Windows.Media.Animation.Clock _animationClock;
    private DispatcherTimer _dayTimer;
    private int _top = 10, _left = 10, _right=200, _bottom=200;
    bool _Maximized = false;
    public const int _DEFAULT_WIDTH = 200;
    public const int _DEFAULT_HEIGHT = 200;
    }


  • jp.m

    You can declare variable with any name .

    But you some times see variables declared like that because of naming convention .

    The _name usually used to represent static variables so that they can easily be identified when browsing through code.

    Thanks.



  • n8dagr8

    Hello Oguz,

    Obviously you can name a variable just about anything you'd like. Some people prefer to use the _ prefix to denote a private variable. This way you know the variable is private in scope just by looking at the name.

    Regards,

    Mike


  • benwild

    Meen zalamak, what is that
  • plusall

    Thank you guys for the explanation.
  • Hossam Abdel Wahab

    This is only a name!


  • chakravarthy_b

    hi,

    its just a naming convenient, which would be followed with class members(variable declared in the class itself to be visible for all methods) , for example if you want to make a property for your class called Name you write it like this


    class Class1

    {
    private string _name;
    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }
    void Method()
    {
    }

    }


    i wanted to creat a property called name , so i need a variable as data repository both of them called Name so the naming convenient is like that the class member will start by "_" or "m_" and the first letter will be small, but the property starts by capital letter, so in your code you can differentiate between them

    hope this helps



  • Question about variable decleration