bcm
bcm

Reputation: 5500

What do 2 brackets in a row mean in xaml.cs?

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();

Upvotes: 1

Views: 293

Answers (3)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60714

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.

Upvotes: 0

twon33
twon33

Reputation: 533

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.

Upvotes: 2

Fredrik Leijon
Fredrik Leijon

Reputation: 2802

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

Upvotes: 3

Related Questions