Home c# Causes of using downcast

Causes of using downcast




What is downcast in C # applies? With upcast everything is clear, but nothing is not clear with it. Why do it do?

Answer 1, Authority 100%

In a well-designed system, the need for a downcast reduction (DOWNCAST) is minimal. However, there are cases in which it is needed. Here are some examples. The usual reason is the weakness of the type system.

  1. Imagine that you want to program a class with a selected instance. But this class can be modified inheriting from him. This leads to about this code:

    namespace system.windows
      Class Application
        STATIC PUBLIC Application Current {Get; Private SET; }
        Public Application ()
          Current = this;
    Namespace MyCoolapp.
      Class App: Application
        Public Mainvm Mainvm {Get; Private SET; }
      Class MyControl
        // ...
        var MainVM = ((App) Application.Current) .mainvm;

    familiar code, is it?

    This has to write because the type system cannot express the requirement to Current it was a topical type so that you can write app.current .

    An attempt to describe the class, which must have a comparison operator with another instance of the same type.

  2. Another valid case of using a downhill – Virtualization by the type of object outside the classes hierarchy.

    Imagine that you need to make a different code behavior for different classes in one inheritance tree. This is usually solved using virtual functions, but what if functionality is not related to the object itself?

    Suppose you have hierarchy of types of geometric shapes, and you want to add “on the side” method of printing to the console. To make this method abstract in the base class does not make sense, because the conclusion on the console is not directly related to the geometric shape. Therefore, you have to write a third-party method, find out the runtime type of object and lead to it.

    Another place where you have to do this – Visitor pattern with extremely ugly classic implementation (without Dynamic ).

    It is probably also possible to consider the weakness of the types of types, at least in future versions of the language This should be solved by other means.

  3. Another case is an existing code, to the “common denominator” of which you have to give your code. For example, a lot of code relies on the InotifyPropertyChanged interface, in which properties are presented to the widest type – Object . So, to work with a real value you need downcast. In fact, it would be possible to abandon the inepitted incotifyPropertyChanged , and use the iobservable & lt; T & GT; for each property separately, but first will have to persuade Microsoft to rewrite WPF!

  4. Well, the last case I see is the need to “patch” the hole that cannot be patched with the right means due to external conditions. For example, you need to remake the classes hierarchy, but Dedine tomorrow. Or the code of the derived class is supplied by another department. Here it may be necessary to break the LSP and do something differently for a specific generated class.

Answer 2, Authority 31%

Well, in general, this is where it can be useful. For such a solution, there is even a special operator:

foo foo = (foo) someobject;

An example of where it can be useful is the case of working with unrelevant collections:

list lst = new list () {new foo ()};
Foo Foo = (Foo) LST [0];

But in general, it should be noted that such a bailiff is actually often not very good practice, as it is fraught with errors in the ranktime. In particular, if we talk about the use of this technique, the use of this technique is the necessary evil, because for one reason or another in the versions of the .NET & LT; 2.0 There were no generalized collections, and therefore, on the fact, all such collections kept references to Object with all the problems arising from here. If LST [0] has some other type, an exception will be generated.

z. By the way, don’t you confuse downcast (bring the ancestor to the heir) and Upcast (bringing the heir to the ancestor)?

Answer 3, Authority 25%

The most common situation, when we know exactly the type of object, but for some reason we get it with the Object type. Standard situations:

  • In event handlers, it is customary to send Object Sender , but in most cases, signing an event, for example, in the WinForms application, we know exactly who will be sender. For example, if we know that this is one of the textbox in the array, then we give it to TextBox and make what we need.

  • Interface ICLONABLE . The clone method returns the object of the same type that the source (well, if it does not do it, then it is necessary to learn anyone to someone), however, it is declared from the Object type. Here without bringing at all in any way.

  • Outdated collections (which are not generic ‘) everything is done through the object, but using them probably adds something specific in them, and not a porridge from everything in a row.

Answer 4, Authority 19%

Reasons, in fact, simple enough. We need to access certain behavior, which is implemented only in the heir class. The current architecture does not allow this to do with virtual methods. It turns out that other options besides specializing type we do not remain. For a more detailed answer, you need an example using-case, which caused this question.

Answer 5

You can add an example to the above, with different data sources. When, for example, there is a mobile application and a web service supported. At some point, we decided to write a new service, but we do not want to refuse the old (customers of our company have old versions of the service, and the new buy does not want). Then, in order to realize additional features in a mobile application, so as not to rewrite the mobile application dramatically, it is necessary to push the objects that differ as a cat and whale.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions