Thread: if (Obj) do

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    if (Obj) do

    This is one of those trivia topics about the language features.

    In C you could do
    Code:
    if (obj) {...}
    In C# you have to do
    Code:
    if (obj != null) {...}
    The funny thing is that C is a more object oriented style approach. It practically uses an overloaded if() when C# has an if for only logical operations and bool values (not for pointers and references).

    If there had to be a separation between bool and references, I would choose to have the bool variables have a == true in an if. Makes more sense for me. You read if(obj) as "if this object (exists)" rather than "if this obj is true". And if (obj != null) would read "if the value of this obj is not equal to null" mixing objects with values.

    What do you think? Except the obvious answer "I have better things to do in my life"

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I think it's just an obvious extension of how C# handles if (). C doesn't use an "overloaded" if at all, it is only following its language construct of 0 for false, anything else true. Even though if (obj != null) is a bit longer to type, its also clearer in its intentions. Not to mention I think overall if you've spent a deal of time in c#, most programming tasks are still quicker to write then in C.

    Either way "that's the way the cookie crumbles". =-D

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Someone's clearly missing the point.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by MWAAAHAAA View Post
    Someone's clearly missing the point.
    That could be any one of us, not clear who you mean

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I also often wonder why C# won't allow if (obj) and haven't yet found a good reason why it does not allow it. Obviously 'if' statements are evaluated a bit differently but it does annoy me that I must use the longer version.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could do like this:
    Code:
    public class Test
    {
    	public Test()
    	{
    	}
    
    	public static implicit operator bool(Test t)
    	{
    		return (t != null);
    	}
    }
    
    var t = new Test();
    
    if(t)
    {
    	System.Windows.Forms.MessageBox.Show("Hey it's true!");
    }
    
    t = null;
    
    if(!t)
    {
    	System.Windows.Forms.MessageBox.Show("And now it's false!");
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Couldn't that be built in? Well, MS I guess had its reasons but I simply cannot imagine them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speeding Up Obj Loader for OpenGL
    By Matty_Alan in forum Game Programming
    Replies: 20
    Last Post: 04-26-2010, 11:23 AM
  2. how to use 'this' for different obj in class
    By effa in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2009, 08:01 PM
  3. using set of class obj to sort by obj member
    By lawrenced in forum C++ Programming
    Replies: 13
    Last Post: 07-24-2009, 03:48 AM
  4. precompiled OBJ files
    By Mastadex in forum Windows Programming
    Replies: 2
    Last Post: 04-18-2006, 09:21 AM
  5. OBJ TDS files .. what are they ?
    By moonwalker in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2002, 02:21 PM