"Prefix" and "how to" cast

This seems to be a very stupid casting question, but ...

I have the following setup:

There are a number of classes derived from the main class. At some point in time and space, I get an element and want to treat it as an object of a subclass.

involves:

class subClassA : mainclass class subClassB : mainclass 

Now I have a question, if you ask which class it is:

 if(someObject is subClassA) { subClassA aA = someObject as subClassA ; } 

While the correct object is returned for most subclasses, I get a null -Object for one subclass.

If I do the following:

 if(someObject is subClassA) { subClassA aA = someObject as subClassA ; // <- aA = null; someObject is shown in debugger as object of subClassA object test = someObject as subClassA; // <- object is not null // or subClassA aB = (subClassA)someObject; // <- object is not null, shows the correct object } 

I have results in test and aB .

What? I do not understand:

Why does as fail and prefix succeed?


Now I am completely lost.

 if(someObject is subClassA) { subClassA aA = someObject as subClassA ; // <- aA = null; someObject is shown in debugger as object of subClassA subClassA aB = someObject as subClassA ; // <- aB != null. } if(someObject is subClassA) { subClassA aB = someObject as subClassA ; // <- aB != null. } 

The name aA is defined locally. Only one thread accesses a method. If I just rename aA, it works.

+6
source share
3 answers

The script you are describing is confused, to say the least, so you can try the following:

 subClassA aA = (someObject as subClassA) ?? (subClassA)someObject; 

Does this work (e.g. aA not null)?

You can refer to the following post for more information about as :

Casting vs using the 'as' keyword in the CLR

Some more research, but I'm not sure how to recreate the script ...

EDIT:

Reading a lot from some very smart people ( Skeet and Lippert ) trying to find the answer ...

See is documenation:

http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.110).aspx

as / conversion documentation:

http://msdn.microsoft.com/en-us/library/ms173105.aspx

+1
source

From MSDN , "as" is equivalent to:

 expression is type ? (type)expression : (type)null 

So, according to MSDN, in your code:

 if(someObject is subClassA) { subClassA aA = someObject as subClassA ; } 

equivalently

 if(someObject is subClassA) { subClassA aA = someObject is subClassA ? (subClassA)someObject : (subClassA)null ; } 

Since someObject is subClassA appears on both lines, the only thing I would think would be either that 1) someObject is actually in some case null; or 2) you have a multi-threaded application, and your code is not thread safe (that is, from time to time the "is" line is true, but by the time the "how" line is reached, the implicit "will" has become false.)

0
source

While in most cases the correct object is returned, sometimes I get a null-Object.

There is only one simple explanation for your problem. The someObject link is being changed by another thread. To protect it, you need to use a lock statement.

0
source

All Articles