Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. 'is' operator

'is' operator

Scheduled Pinned Locked Moved C#
questionhelplounge
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 1033907
    wrote on last edited by
    #1

    The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:

    object x = FunctionThatReturnsAnInteger();
    if(x is decimal)
    Console.WriteLine("Hello world");

    The above condition is never true because x is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.

    C C P 3 Replies Last reply
    0
    • M Member 1033907

      The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:

      object x = FunctionThatReturnsAnInteger();
      if(x is decimal)
      Console.WriteLine("Hello world");

      The above condition is never true because x is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      These are integral types, is only works for classes. This should not compile. The is operator WILL work with a class structure. class Base class Derived1 : Base Derived1 n; if ( n is Base ) { // this will work }

      Member 1033907 wrote:

      what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal?

      I suspect you need your try/catch block. You also have the cost of boxing to contend with. You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      M 1 Reply Last reply
      0
      • C Christian Graus

        These are integral types, is only works for classes. This should not compile. The is operator WILL work with a class structure. class Base class Derived1 : Base Derived1 n; if ( n is Base ) { // this will work }

        Member 1033907 wrote:

        what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal?

        I suspect you need your try/catch block. You also have the cost of boxing to contend with. You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.

        Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

        M Offline
        M Offline
        Member 1033907
        wrote on last edited by
        #3

        Thanks for your reply.

        Christian Graus wrote:

        This should not compile.

        I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.

        Christian Graus wrote:

        You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.

        String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.

        C 2 Replies Last reply
        0
        • M Member 1033907

          Thanks for your reply.

          Christian Graus wrote:

          This should not compile.

          I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.

          Christian Graus wrote:

          You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.

          String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Member 1033907 wrote:

          I compiles fine, 'is' works with structures as well.

          I didn't think that was the case. I know I've had 'is' not compile for me before because I used an integral type.

          Member 1033907 wrote:

          String parsing seems to me like the slowest possible approach

          Well, you're already paying the cost of boxing, which is expensive, and talking about try/catch, which is VERY expensive.

          Member 1033907 wrote:

          I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.

          I would do some testing to see which is faster.

          Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

          1 Reply Last reply
          0
          • M Member 1033907

            Thanks for your reply.

            Christian Graus wrote:

            This should not compile.

            I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.

            Christian Graus wrote:

            You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.

            String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            I've got it. It's 'as' that doesn't work, because int cannot be null. But, is and as both rely on an inheritance tree, they do not try to do any sort of conversion between types. decimal does not derive from int, or vice versa, so neither of those will work. You're asking if the type can be converted, not if it is of that type to start with.

            Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

            1 Reply Last reply
            0
            • M Member 1033907

              The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:

              object x = FunctionThatReturnsAnInteger();
              if(x is decimal)
              Console.WriteLine("Hello world");

              The above condition is never true because x is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Member 1033907 wrote:

              int can be cast to decimal without an exception being thrown - so why does it not work?

              Just because you can cast to it does not mean it is part of the same type heirarchy. Types can provide their own casting operators to make coercion to other types easier for the programmer. is/as only work on things when they are along the same branch of an object heirarchy. decimal derives from object. int derives from object. Each is heading down different branches.

              Man who stand on hill with mouth open wait long time for roast duck to drop in

              1 Reply Last reply
              0
              • M Member 1033907

                The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:

                object x = FunctionThatReturnsAnInteger();
                if(x is decimal)
                Console.WriteLine("Hello world");

                The above condition is never true because x is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work? More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better. Thanks for sugegstions, H.

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                The term "cast" is used to mean two different things, one of which is more like "convert". When an int is "cast" to decimal it gets converted. The is operator doesn't work with that type of cast.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups