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. Info about casting

Info about casting

Scheduled Pinned Locked Moved C#
questiontutorial
5 Posts 3 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.
  • S Offline
    S Offline
    Saksida Bojan
    wrote on last edited by
    #1

    Hello I would like to know difference betwen casting and if there is one, then in which conditions shroud be used Example: TextBox txt = new TextBox; Object obj = txt; // What is the difference between these 2 castings TextBox txt1 = (TextBox) txt; TextBox txt2 = txt as TextBox; This may be stupid question but thanks in advance

    A 1 Reply Last reply
    0
    • S Saksida Bojan

      Hello I would like to know difference betwen casting and if there is one, then in which conditions shroud be used Example: TextBox txt = new TextBox; Object obj = txt; // What is the difference between these 2 castings TextBox txt1 = (TextBox) txt; TextBox txt2 = txt as TextBox; This may be stupid question but thanks in advance

      A Offline
      A Offline
      Andrew Rissing
      wrote on last edited by
      #2

      Check this out: http://www.c-sharpcorner.com/UploadFile/pcurnow/polymorphcasting06222007131659PM/polymorphcasting.aspx[^]

      A 1 Reply Last reply
      0
      • A Andrew Rissing

        Check this out: http://www.c-sharpcorner.com/UploadFile/pcurnow/polymorphcasting06222007131659PM/polymorphcasting.aspx[^]

        A Offline
        A Offline
        Andrew Rissing
        wrote on last edited by
        #3

        And in general, I prefer using 'as', but it depends on your situation. If you want an exception to be thrown, use explicit casting (ex. "((TextBox)obj)"). Generally, its good to avoid exceptions unless there's just nothing you can do to avoid the 'exceptional' behavior.

        S 1 Reply Last reply
        0
        • A Andrew Rissing

          And in general, I prefer using 'as', but it depends on your situation. If you want an exception to be thrown, use explicit casting (ex. "((TextBox)obj)"). Generally, its good to avoid exceptions unless there's just nothing you can do to avoid the 'exceptional' behavior.

          S Offline
          S Offline
          Saksida Bojan
          wrote on last edited by
          #4

          Thanks to both replies. Those answer extinguish my curiosty

          L 1 Reply Last reply
          0
          • S Saksida Bojan

            Thanks to both replies. Those answer extinguish my curiosty

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Also, the C-style cast can be overloaded, but as can not be overloaded.

            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