In this example, the person variable references an instance of the Employee class. The following downcast will fail at runtime: var employee = new Employee()Ĭandidate candidate = (Candidate)person // downcast fail at runtime Code language: C# ( cs ) The following defines the Candidate class that inherits from the Person class: class Person Since the employee2 variable refers to the Employee object, it can access all properties of the Employee‘s object. In this example, the downcast operation explicitly converts the object referenced by the person reference to the employee2. The following example illustrates the downcasting: var employee = new Employee()Įmployee employee2 = (Employee)person // downcastĬonsole.WriteLine(employee2.JobTitle) Code language: C# ( cs ) If no explicit conversion exists from the type of E to type T, the compiler will raise an error.Īlso, at runtime, the explicit conversion might fail and the cast expression might throw an exception. The cast expression explicitly converts the result of the expression ( E) to the type T. A downcast requires a cast expression with the following syntax: (T)E Code language: C# ( cs ) Code language: plaintext ( plaintext ) DowncastingĪ downcast operation creates a subclass reference from a base class reference. The following statement will cause a compile-time error: person.JobTitle Code language: C# ( cs )Įrror: 'Person' does not contain a definition for 'JobTitle'. For example, it can only access the Name property but cannot access the JobTitle property of the object. The object itself does not change.Īlthough the person and employee variables refer to the same object, the person has a more restrictive view on that object. Both employee and person variable reference the same Employee‘s object. Third, assign the object referenced by the employee variable to a reference of the Person class.Īfter upcasting, the variable person still references the same Employee‘s object.Second, create a new instance of the Employee class and assign it to the employee variable.The Person class is a base class and the Employee class is a subclass. First, define the Employee class that inherits from the Person class.Person person = employee Code language: C# ( cs ) } Code language: C# ( cs ) var employee = new Employee() A downcast only succeeds if the objects are compatible types.Īn upcast creates a base class reference from a subclass reference. Explicitly downcast to a subclass reference.Implicitly upcast to a base class reference.Also, you cannot reassign a value of another type to the variable unless that type is implicitly compatible with the variable’s type. It means that after you declare a variable, you cannot redeclare it. Introduction to C# castingĬ# is a statically-typed programming language. If it is used improperly, it could produce undefined behavior.Ī popular example of a badly considered design is containers of top types, like the Java containers before Java generics were introduced, which requires downcasting of the contained objects so that they can be used again.Summary: in this tutorial, you’ll learn about the C# casting including upcasting and downcasting. Compile-time downcasting is implemented by static_cast, but this operation performs no type check. In C++, run-time type checking is implemented through dynamic_cast. While we could also convert myObject to a compile-time String using the universal (), this would risk calling the default implementation of toString() where it was unhelpful or insecure, and exception handling could not prevent this. In this approach, downcasting prevents the compiler from detecting a possible error and instead causes a run-time error.ĭowncasting myObject to String ('(String)myObject') was not possible at compile time because there are times that myObject is String type, so only at run time can we figure out whether the parameter passed in is logical. Public static String objectToString ( Object myObject )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |