C# 'that' keyword

My younger brother, Danny, is doing the same graduate program that I did at the University of Florida. Recently he was given a pop quiz from one of my favorite professors where he was asked to describe what the that keyword does in C#. I must admit - I was also initially stumped because I didn't expect it to be a trick question... I figured maybe, since I focus on J#, I just hadn't encountered the C# keyword (perhaps it was part of C# 2.0 and I hadn't noticed). Ok, ok, so really I have no good excuse...

But my question for the readers of my blog: If there was a that keyword for C#, what would it be used for? Accepting all answers, real or absurd. I'll post the best ones here. BrianKel at Microsoft dot com. Who knows, maybe I'll ask Dan to champion the best responses with Anders to consider for C# 3.0? :-)

Comments

  • Anonymous
    September 22, 2004
    I can't think of a logical reason to add it, it would be superfluous and long winded.

    What would you use it for?

    My only idea would be this:

    foreach (MyType a that IBlarg in myCollection)
    {
    //and get just the MyType's that support IBlarg as MyType?
    }

    Which, to be honest, is awful and just lazy but it would save on an if statement... It would let you pull out a set of items that support a particular interface or inherit something.

    I certainly wouldn't WANT it or not want it, but I wouldn't be averse to using it if it was doing something timesaving.

  • Anonymous
    September 22, 2004
    "that" keyword in C# ? :S

    I never heard of this...what it can refer to to? which instance? :-)

  • Anonymous
    September 22, 2004
    Three initial thoughts:

    "that" could be used as a keyword in a with statement.

    public int ThatExample<T>(T x, T y) {
    with(System.Collections.Comparer<int>.Default) {
    if(that.Compare(x, y) > 0) {
    return that.GetHashCode(x);
    }
    else {
    return that.GetHashCode(y);
    }
    }
    }

    This usage doesn't really gain you anything over declaring a variable called "that", except for the implicit typing of "that". However, Anders has already discussed a possible solution to this problem (var).


    My second idea for "that" is an implied variable referring to the return value of a function, similar to VB's ability to refer to the return value by the method's name (although it'd be much more concise that repeating the method name over and over).

    Example (same method as above, but using a different definition of "that")

    public int ThatExample<T>(T x, T, y) {
    IComparer<T> comparer = Comparer<T>.Default;
    if(comparer.Compare(x, y) > 0) {
    that = comparer.GetHashCode(x);
    }
    else {
    that = comparer.GetHashCode(y);
    }
    }

    I think this (errr...that) could be more useful. It is often nice in VB to be able refer to the return value directly without declaring a variable. This would be especially useful in factory methods that need to create an object, set lots of properties, call lots of methods, and return the new object. In this case, "that" is also implicitly typed to the return type of the method.

    My third idea involves double virtual dispatch, but I cannot come up with a good example or syntax. Besides, I'm not sure how easily double-dispatch could be implemented in C# and/or the CLR.

  • Anonymous
    September 22, 2004
    It's part of the debugger support in the CLR. When debugging a problem you can't track down you may well see references to

    that.methodCall()

    where the that keyword refers to the actual object you should be using, instead of the one you mistakenly codeded.

  • Anonymous
    September 22, 2004
    The that Keyword, yes its in the same namespace as the SoWhatIf condition isnt it ?

  • Anonymous
    September 22, 2004
    it sounds to me as equivalent of VB.NET "My" keyword ;)

  • Anonymous
    September 22, 2004
    Never heard of the 'that' keyword, but wouldn't it just be fun if it was the C# term for 'My'. After all 'this' is this and 'that' would obviously be that.

    lol

    ;)

  • Anonymous
    September 23, 2004
    The 'that' keyword is an implicit reference to a copy of the 'this' object that is used when making deep copies (via .Clone() or an equivalent). So if you wanted to deep copy an object like:
    class X : ICloneable {
    private SomeOtherDeepCopyableClass data;

    [UsesCopy]
    public override object Clone() {
    that.data=(SomeOtherDeepCopyableClass)data.Clone();
    }
    }

    Note that 'that' is enabled by the "UsesCopy" attribute and is implicitly returned if no other return statement is encountered.

  • Anonymous
    September 23, 2004
    int i that playsgolf();

    I don't know what it could do, but an integer that played golf could prove to be entertaining--at least for a few seconds.

  • Anonymous
    September 23, 2004
    The that keyword is short hand for what is on the left hand side of the equals sign, a = that + b. It gets mildly confusing when you have a boolean expression a == b + that == c + that. The first that is equal to a and the second that is equal to b + a.

  • Anonymous
    September 23, 2004
    I think it would be perfect for any binary operator overloading. Now you dont have to two arguments. You will endup referencing it implicitly.

    this.ID == that.ID ..

    Also, it can probably be used in AOP (if .NET supports it v5.0 ;)) to provide modification semantics (deleted, inserted) similar to SQL triggers.

  • Anonymous
    September 23, 2004
    Could be useful in operator overloading. Binary operators. People often use a variable "rhs" or something. "that" could be used to make it implicit.

  • Anonymous
    September 23, 2004
    Maybe testing object equality? I don't mean the values in the object A match the values in object B, I mean A and B point to the same object in memory. Maybe syntax like:
    if(A is that B){...}

  • Anonymous
    September 23, 2004
    The comment has been removed

  • Anonymous
    September 23, 2004
    it sounds like a VB.NET "My" equivalent,
    and please dont't refer to Dan ;)

  • Anonymous
    September 25, 2004
    The comment has been removed

  • Anonymous
    September 26, 2004
    I found this in the docs:

    The "that" keyword enables a developer to easily refer to another arbitrary object when focusing their attention on the other, more important objects at hand. The runtime will attempt to pick an object that fits within context (such as when "that" is used as a parameter to a method), but, failing that, will choose a class with a name that shares some of the same letters.

    Be careful not to overuse the "that" keyword in that the optimizing compiler will often share global pointer space between "that" objects and objects referred to via "the_other_thing", resulting in volatile conditions when multiple applications are simultaneously using other classes in System.Abstraction.Obfuscation.Indirection.

    Also be aware of "that"'s default requirement of 66 bits, of which bits 0-31 are used as an address pointer, 32-63 are used as a redundant backup copy of bits 0-31, bit 64 is a flag that indicates if "that" is currently in use while bit 65 tells you whether or not the value in bit 64 must be inverted before usage.

    Hope this helps.

  • Anonymous
    September 27, 2004
    Just to give C# developers more rope to hang themselves with, let’s make "that" an object reference to the caller. And you thought "typeof" was dangerous...

    We could implement the "friend" concept at runtime or, even better, the "private" access modifier:

    public class B
    {
    public void CallFriend()
    {
    class A = new A();
    A.SharedWithB();
    }
    }

    public class A
    {
    public void SharedWithB()
    {
    if (typeof(that) != typeof(B))
    {
    throw new He'sNotAFriendException();
    }
    }

    public void PrivateMethod()
    {
    if (this != that)
    {
    Throw new Wait,That’sNotMe!Exception();
    }
    }
    }

  • Anonymous
    October 09, 2004
    The comment has been removed

  • Anonymous
    June 14, 2005
    C#ではThat(VB新機能のMy に対するwrapper)

  • Anonymous
    June 18, 2007
    PingBack from http://www.pistalwhipped.net/bl/og/2007/06/18/that-keyword/

  • Anonymous
    May 31, 2009
    PingBack from http://outdoorceilingfansite.info/story.php?id=3863