Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by jashan · Sep 11, 2010 at 01:08 PM · scriptingbasicscasting

What is the difference between "prefix-casting" (Type)variable - and "as-casting" (variable as Type)?

I know there's two ways of casting in C#:

Prefix-casting:

MyType newVariable = (MyType) myVariable;

As-casting:

MyType newVariable = myVariable as MyType;

What is the difference?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by jashan · Sep 11, 2010 at 01:08 PM

Prefix-casting is usually quite a bit safer because if you run into an exception, it gives you a TypeCastException telling you exactly what's going on. As-casting on the other hand will simply return null if that cast is not allowed. This creates a couple of difficulties when there is a problem: a) you don't know if myVariable was null or of the wrong type, b) newVariable might be null and this might give you a NullReferenceException at a later point in time ... which might make debugging harder.

However, as-casting has one significant advantage: It's much faster. In most cases, this "much faster" won't matter much, but if you're on the iPhone or Android, or when you're doing those casts thousands of times per frame, it may make a difference.

In both cases, it's good practice to add some checks or exception handling; depending on what your exact needs are:

if (myVariable is MyType) {
    MyType newVariable = (MyType) myVariable;
    // ... do whatever you wanna do with newVariable
}

Or

try {
    MyType newVariable = (MyType) myVariable;
    // ... do whatever you wanna do with newVariable
} catch (InvalidCastException exc) {
    // do whatever needs to be done if that exception occurs
}

Or

if (myVariable is MyType) {
    MyType newVariable = myVariable as MyType;
    // ... do whatever you wanna do with newVariable
}

Or

MyType newVariable = myVariable as MyType;
if (newVariable != null) {
    // ... do whatever you wanna do with newVariable
} else {
    // ignore or send a warning or whatever you need to
    // do when either myVariable is null or not of the
    // correct type
}

In any case, it helps when you understand exactly what you're doing ;-)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image StephanK · Dec 03, 2015 at 08:05 AM 0
Share

Actually it's not true that as-casting is faster. The difference between the two is very tiny but if you want to make the distinction: prefix-casting is faster than as-casting.

avatar image mabakay · Dec 07, 2016 at 01:31 PM 0
Share

Second example with try...catch is worst of all.

avatar image
0

Answer by aerinzero · Apr 08, 2012 at 03:08 PM

Another difference I noticed: (Unity 3.5, C#)

//data is an arrayList with some Vector3's and other types stored in it. Vector3 spawn = data[0] as Vector3; //THIS FAILS Vector3 spawn = (Vector3)data[0]; // THIS WORKS

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mabakay · Dec 07, 2016 at 01:30 PM 0
Share

Becouse Vector3 is a value type and these can't have a value of null. If you would cast "as Vector3?" than everything will work just fine. It's not a Unity connected thing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can someone explain 'Using ..." and "MonoBehaviour" in C# 7 Answers

Can some one help me with splash screen? 1 Answer

What are the Syntax Differences in C# and Javascript? 7 Answers

How to make door open and close with Input.GetButtonDown? 1 Answer

How to check if variable defined in script is present in shader? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges