SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2010
    Location
    ONS
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does the underscore mean in C Sharp?

    What does the underscore mean in C Sharp?

    I saw a program,

    Code Csharp:
    Class Metals {
    string _metalType;
    public Metals(string type)
    {
    _metalType = type;
    Console.WriteLine("Metal: " + _metalType);
    }
    }

    What does the _ (underscore) mean in it?
    Does that mean any static variable or something else?

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's not a rule, it's more of a convention. I think many folks use it to denote a private member in the class.

  3. #3
    SitePoint Addict dAEk's Avatar
    Join Date
    Oct 2003
    Location
    Stockholm, Sweden
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's another way to refer to private instance variables. Instead of typing this.metalType, some tend to prefer _metalType for some reason. Others use the m_metalType naming convention which is even worse.
    David Shamloo-Ekblad

    Go Habs! | For music addicts: Last.fm, SongMeanings

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,876
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dAEk View Post
    It's another way to refer to private instance variables. Instead of typing this.metalType, some tend to prefer _metalType for some reason. Others use the m_metalType naming convention which is even worse.
    this. is depreciated and not needed. if you really wanted to you could also do: this._metalType

    _ means nothing in C#, it is just the name of the variable, would be exactly the same if you leave the _ out.

  5. #5
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NightStalker-DNS View Post
    this. is depreciated and not needed. if you really wanted to you could also do: this._metalType

    _ means nothing in C#, it is just the name of the variable, would be exactly the same if you leave the _ out.
    I'm not sure if I read it correctly or understand it, but when in the hell did "this.property" become deprecated in C#? Geez, I seem to be always behind the "proper way" curb. Then again, I have been using it in JavaScript and Literal Expressions.

    Good to know, I guess.

  6. #6
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,876
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    You can use it, but it is not needed. It is a redundant qualifier.

    It is only needed if your property has the same name as a method parameter, but that in itself is bad practice

  7. #7
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NightStalker-DNS View Post
    You can use it, but it is not needed. It is a redundant qualifier.

    It is only needed if your property has the same name as a method parameter, but that in itself is bad practice
    Night, do you code in JS, if so, do you use "this"? I thought it was mandatory in order to access objects in Literal form. Or, where you only referring to C#?

  8. #8
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,876
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    I am only referring to C#. I usually use this in javascript as well. Not sure if its redundant or not in js

  9. #9
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NightStalker-DNS View Post
    You can use it, but it is not needed. It is a redundant qualifier.

    It is only needed if your property has the same name as a method parameter, but that in itself is bad practice
    Is it really bad practice? I've learned that way and still use this sort of code:
    Code Csharp:
    private IPageRepository extrasRepository;
    public PageItemsController(IPageRepository extrasRepository)
    {
    	this.extrasRepository = extrasRepository;
    }

  10. #10
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,876
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginekitty View Post
    Is it really bad practice? I've learned that way and still use this sort of code:
    Code Csharp:
    private IPageRepository extrasRepository;
    public PageItemsController(IPageRepository extrasRepository)
    {
    	this.extrasRepository = extrasRepository;
    }
    Well, according to me it is. I would start the private variable with _

  11. #11
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NightStalker-DNS View Post
    Well, according to me it is. I would start the private variable with _
    I don't mind agreeing with you, I just want to know why. Who deprecated it?

  12. #12
    SitePoint Zealot chieftain's Avatar
    Join Date
    Feb 2005
    Location
    Michigan
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just thinks it varies from individual to individual, I do similar to imaginekitty

    private IQueryable theSource;

    public IQueryable TheSource
    {
    get { return theSource; }
    set { theSource = value; }
    }

  13. #13
    Non-Member
    Join Date
    Aug 2010
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    _metalType is just the name of the variable, makes no difference if you write it without underscore like "metalType".


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •