You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

casting.md 2.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ---
  2. uid: Guides.Entities.Casting
  3. title: Casting & Unboxing
  4. ---
  5. # Casting
  6. Casting can be done in many ways, and is the only method to box and unbox types to/from their base definition.
  7. Casting only works for types that inherit the base type that you want to unbox from.
  8. `IUser` cannot be cast to `IMessage`.
  9. > [!NOTE]
  10. > Interfaces **can** be cast to other interfaces, as long as they inherit each other.
  11. > The same goes for reverse casting. As long as some entity can be simplified into what it inherits, your cast will pass.
  12. ## Boxing
  13. A boxed object is the definition of an object that was simplified (or trimmed) by incoming traffic,
  14. but still owns the data of the originally constructed type. Boxing is an implicit operation.
  15. Through casting, we can **unbox** this type, and access the properties that were unaccessible before.
  16. ## Unboxing
  17. Unboxing is the most direct way to access the real definition of an object.
  18. If we want to return a type from its interface, we can unbox it directly.
  19. [!code-csharp[Unboxing](samples/unboxing.cs)]
  20. ## Regular casting
  21. In 'regular' casting, we use the `as` keyword to assign the given type to the object.
  22. If the boxed type can indeed be cast into given type,
  23. it will become said type, and its properties can be accessed.
  24. [!code-csharp[Casting](samples/casting.cs)]
  25. > [!WARNING]
  26. > If the type you're casting to is null, a `NullReferenceException` will be thrown when it's called.
  27. > This makes safety casting much more interesting to use, as it prevents this exception from being thrown.
  28. ## Safety casting
  29. Safety casting makes sure that the type you're trying to cast to can never be null, as it passes checks upon calling them.
  30. There are 3 different ways to safety cast an object:
  31. ### Basic safety casting:
  32. To safety cast an object, all we need to do is check if it is of the member type in a statement.
  33. If this check fails, it will continue below, making sure we don't try to access null.
  34. [!code-csharp[Base](samples/safety-cast.cs)]
  35. ### Object declaration:
  36. Here we declare the object we are casting to,
  37. making it so that you can immediately work with its properties without reassigning through regular casting.
  38. [!code-csharp[Declare](samples/safety-cast-var.cs)]
  39. ### Reverse passage:
  40. In previous examples, we want to let code continue running after the check, or if the check fails.
  41. In this example, the cast will return the entire method (ignoring the latter) upon failure,
  42. and declare the variable for further use into the method:
  43. [!code-csharp[Pass](samples/safety-cast-pass.cs)]
  44. > [!NOTE]
  45. > Usage of `is`, `not` and `as` is required in cast assignment and/or type checks. `==`, `!=` and `=` are invalid assignment,
  46. > as these operators only apply to initialized objects and not their types.