tags:

views:

19

answers:

1

Is it necessary to always check CanFreeze before freezing an object.

If I look at PresentationOptions:Freeze to use in markup, there is it not done. Therefore I dont think, that CanFreeze must be called before everey object-creation.

But when has CanFreeze to be used?

+1  A: 

Hi,

you don't have to call CanFreeze but if you don't and your object is not freezable an InvalidOperationException will be thrown (see the "remarks" section of Freeze()). So CanFreeze is an option to allow you to call Freeze() without risking an exception.

There are several cases in which a call of Freeze() would result in an exception. Direct quote from the Freeze() reference:

You cannot freeze a Freezable object if any one of the following conditions is true about the object:

  • It has animated or data bound properties.

  • It has properties that are set by a dynamic resource. For more information about dynamic resources, see the Resources Overview.

  • It contains Freezable sub-objects that cannot be frozen.

andyp
+1 Yes, but in which case is it necessary? When are problems to be expeceted? If I create a new instance of a SolidColorBrush? Probably not. As I wrote in my post, also MS does it not in its implementation of PresentationOptions:Freeze (if one sets the value to true). My question is, if there are some pitfalls with it, but probably my question is to unspecific. Thanks for the answer anyway.
HCL
Updated my answer to quote several cases in which you could prevent an exception by Freeze() if it's called conditionally (CanFreeze beeing the condition).
andyp