Difference between ''(Texture)'' and ''as Texture''
Hi! I've tried to figure this out on my own using the documentation, but I am still pretty confused and I would like some clarification. I'm trying to figure out what the difference is between:
A. someObj.mainTexture = (Texture)Resources.Load("fooble");
B. someObj.mainTexture = Resources.Load("fooble") as Texture;
and
C. someObj.mainTexture = (Texture)Resources.Load("fooble") as Texture;
All three of the above seem to work the same. Thanks so much, sorry if this is a total n3wb question, but, I am a total n3wb, so, guilty as charged I guess :P
Answer by tanoshimi · Aug 05, 2016 at 12:46 PM
When they work, the result is identical. They differ when, for whatever reason, "fooble" cannot be converted to a Texture. In such cases, a.) will thrown an exception, while b.) will return a null value.
There are a few other differences too, as explained here: https://blogs.msdn.microsoft.com/ericlippert/2009/10/08/whats-the-difference-between-as-and-cast-operators/