views:

66

answers:

3

Just started learning C# (in xaml), so appreciate some elaboration:

((MSAvalon.Windows.Serialization.ILoaded)(_Text_2_)).DeferLoad();

Not sure what all the round brackets means... does it mean "_Text_2_" is a child of object "MSAvalon.Windows.Serialization.ILoaded" ?

and if so... why not just something like:

MSAvalon.Windows.Serialization.ILoaded._Text_2.DeferLoad();
+3  A: 

_Text_2_ is casted to a MSAvalon.Windows.Serialization.ILoaded Object/Interface and then the method DeferLoad is called

Fredrik Leijon
will this work also? **(CastType)_Text_2_.DeferLoad()**
Brandon
No, but this will: [((CastType)_Text_2_).DeferLoad();] If you do it the other way, you're casting the result of DeferLoad() which is probably not what you want.
twon33
good point twon33
Brandon
to further my understanding of brackets... how do < > brackets work in C#? what's the term I should search for to understand this.
Brandon
< > Is used with generics, to specify what kind of class it will accept/return
Fredrik Leijon
thanks Fredrik, your comment makes more sense after I did some reading on what "generics" and "angle brackets" mean in C#.
Brandon
+2  A: 

This is a typecast, used to tell the compiler that a variable is of a particular type when it's not obvious. It could have been used like this:

class Foo {
    private object _Text_2_;

    void Method() {
        ((MSAvalon.Windows.Serialization.ILoaded)_Text_2_).DeferLoad();
    }
}

Leaving out the typecast here would cause a compiler error, since DeferLoad is not a method of object. You're telling the compiler here that you have some special knowledge that _Text_2_ is really what you say it is.

twon33
A: 

Basically it's started with (( so that DeferLoad can be called. I'll illustrate with an example.

Lets say you do the following.

object s = "Hello world";

s now contains a string. If you want to make this string uppercase using a cast (as in your example, I can't simply write this

(string)s.ToUpper();

ToUpper() can't be called, since it's not valid on a variable of type object. If you rewrite this to

((string)s).ToUpper()

it's valid. Because of the brackets, s is casted to a string first, then string.ToUpper() is called on the variable.

Note that in this case (s as string).ToUpper() would be a cleaner approach.

Øyvind Bråthen
Why is '(s as string).ToUpper()' a cleaner approach? If s isn't a string, you will get a NullReferenceException instead of InvalidCastException. I dont think this is cleaner.
PetPaulsen
Cleaner as in that I think the code is more readable, since one layer of brackets is removed. To avoid the exception you could use s.ToString().ToUpper(), but if you only wanted this to be done if s was in fact a string, you could do something like thisstring str = s as string;if (str != null){ (s as string).ToUpper();}
Øyvind Bråthen
That code did not look good in a comment I see after saving it, but you get the point I hope :)
Øyvind Bråthen
"as" is really for when you expect the cast to fail during normal program flow. PetPaulsen is really right here; you shouldn't use it just to save parentheses, since the resulting NullReferenceException yields less information about what the real problem is than the InvalidCastException you'd get with the parentheses.
twon33
I will have to agree with PetPaulsen and twon33 here. I didn't consider that the NullReferenceException gives less information than the InvalidCastException, so good call guys.
Øyvind Bråthen