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
0
Question by Hamlet-Archer · May 07, 2013 at 06:54 AM · classtypesyntaxis

if (!(apple is Vege)) then...

this is a syntax question. is there a neater way to write:

public Fruit apple; // Fruit and Vege are my classes

if (!(apple is Vege)) // do something

something like:

if (apple !is Vege) // do something

would be ideal.

Comment
Add comment · Show 2
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 bodec · May 07, 2013 at 07:03 AM 0
Share
 if(!apple == Vega){
 do somthing
 }
avatar image Hamlet-Archer · May 07, 2013 at 07:05 AM 0
Share

sorry i wasn't clear... apple is a variable of type Fruit, and both Fruit and Vege are classes. so (apple is Vege) is a check of whether apple is of type Vege.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · May 07, 2013 at 08:03 AM

Actually it is always faster to write:

 var vege = apple as Vege;
 if(vege != null) ...

Presuming you are going to use the Vege class. But it's not any neater :)

You have the neatest syntax as ugly as it may be.

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 whydoidoit · May 07, 2013 at 08:05 AM 0
Share

You could write an extension method, probably slower:

     public static class TypeHelper
     {
           public static bool Isnt<T>(this object test)
           {
               return !typeof(T).IsAssignableFrom(test.GetType());
           }

 
     }


    if(someObject.Isnt<Vege>())
avatar image Hamlet-Archer · May 07, 2013 at 08:18 AM 0
Share

thanks buddy. you're right that the assignment method works well if one is going to use it afterwards. though since i'm just checking the type, all the syntaxes so far look just about as good as each other.

the extension method works (except for needing to complicate things with an extension), though i am surprise to find that c# doesn't already have that built-in somewhere. anyway, thanks!

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to store class type in variable 2 Answers

Public class used in another class (C#) 1 Answer

Get Superclass of Subclass in Unityscript 2 Answers

calling a class named by a string -1 Answers

Class hierarchy, gameObject, Raycast 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