C# FAQ: What is the difference between using a cast and the as operator


Jump to: navigation, search

What is the difference between using a cast and the as operator?

Performing an explicit cast differs from using the as operator in three major aspects.

The as operator…

  1. returns a null if the variable being converted is not of the requested type nor present in its inheritance chain. On the other hand, a cast would throw an exception.
  2. can be applied only to reference type variables being converted to reference types.
  3. cannot perform user-defined conversions—e.g., explicit or implicit conversion operators. A cast could perform these types of conversions.

Far from being merely cosmetic, the as operator and casting are handled by two seperate operations defined in Intermediate Language (IL)—the castclass and isinst IL instructions.

Developers report that the as operator is slightly faster than casting in C#.


More CSharp Language FAQs

Go to Visual CSharp FAQ (main page).

Consider these related Frequently Asked Questions—FAQs:

  1. Are C# constructors inherited?
  2. Are C# parameters passed by reference or by value ?
  3. Can a property have different get and set access?
  4. Can const and static be used together?
  5. Can I directly call a native function exported from a DLL?
  6. Do unused references to external assemblies lead to code bloat?
  7. Does C# have its own class library?
  8. Does C# have macros or a preprocessor?
  9. Does C# support custom exceptions?
  10. Does C# support jagged arrays?
  11. Does C# support variable method arguments (vararg)?
  12. How call overloaded operators from non-supporting languages?
  13. How can I force garbage collection?
  14. How can I output simple debugging messages?
  15. How can one constructor call another?
  16. How check the object type at runtime?
  17. How can type name clashes be resolved?
  18. How do destructors work in C#?
  19. How do I declare an out variable?
  20. How do I get DllImport to work?
  21. How do I make a C# DLL?
  22. How do I use trace and assert?
  23. How get the type name at runtime?
  24. How keep a local variable in scope across a try-catch block?
  25. How make sure C# classes will interoperate with other .NET languages?
  26. How perform a case insensitive string comparison?
  27. How process command line arguments?
  28. How specify a C# literal type?
  29. How subscribe to events exposed by remoted objects?
  30. How to obtain type information on the fly using typeof?
  31. How use an alias for a class or namespace?
  32. How would I create a Delegate or MulticastDelegate?
  33. Is there C# support for C-type macros?
  34. Is there C# support for templates?
  35. What are the C# character escape sequences (\)?
  36. What does at sign (@) identifier mean?
  37. What does "Object reference not set to an instance of an object" mean?
  38. What is a namespace?
  39. What is a type alias?
  40. What is C#?
  41. What is a C# response file?
  42. What is a verbatim string literal (@)?
  43. What is the C# Language Specification?
  44. What is the #define preprocessor directive?
  45. What is the difference between const and static readonly?
  46. What is the difference between delegate and event?
  47. What is the difference between GetType() and typeof?
  48. What is the difference between new and override?
  49. What is the difference between the out and ref parameter modifiers?
  50. What is the difference between string and System.String?
  51. What is the difference between using a cast and the as operator?
  52. What is the syntax for calling an overloaded constructor from within a constructor?
  53. What is the use of the C# compiler's /target: command line option?
  54. What is the Windows Class Viewer?
  55. What software is needed to develop C# applications?
  56. When should an exception be thrown?
  57. When should Equals and == be used?
  58. Will Console.WriteLine() stop printing upon encountering a NULL character in a string?
  59. Why add a using statement and a reference?
  60. Why are hashtable lookups so slow with struct keys?
  61. Why do I get a syntax error when I try to declare a variable named checked?
  62. Why does C# not support checked exceptions?
  63. Why does my Windows application open a console window each time it is run?
  64. Why doesn't the C# switch statement work like I expect it to?
  65. Why doesn't a Windows Form project use visual styles in XP?
  66. Why get an error (CS1006) when declaring a method without a return type?
  67. Why must struct constructors have at least one argument?





Personal tools
Share this page
  • del.icio.us
  • Facebook
  • StumbleUpon