.Net Generics... Just a class definition



  • Ok ... This line compiles... But do you think it makes sense?

     

    public<font size="2"> </font><font color="#0000ff" size="2">class</font><font size="2"> </font><font color="#008080" size="2">Kappa</font><font size="2"> : Whatever.</font><font color="#008080" size="2">BaseS</font><font size="2"><</font><font color="#008080" size="2">Kappa</font><font size="2">></font>



  • odd. What happens when you use it? Does your computer asplode?



  • This is quite common in Java.  It's easier to ingest if you remember that the notation is just telling the compiler to treat some superclass methods, at compile-time, as if they deal with only a specific type.  (I'm assuming that .NET generics work in a manner similar or identical to Java generics.  I don't actually know if that's the case.)

     



  • Well it compiles...

     

    I was trying to create a "SingeltonBase" that would allow me to inherit "singelton behaviour"... There is only a slight glitch in the logic for .Net... The "base" must create a master class... This means the Master needs a public constructor (<T> where T:new()) . But Ill try to get around this limitation sometime later (with reflection)



  • that's nothing, take a look at this:

    public class test<E extends test<E>> extends java.util.ArrayList<test>{}

     

    perfectly valid and compiling java (using sun's JDK1.5) 



  • [quote user="rdrunner"]

    Ok ... This line compiles... But do you think it makes sense?

    public<font size="2"> </font><font color="#0000ff" size="2">class</font><font size="2"> </font><font color="#008080" size="2">Kappa</font><font size="2"> : Whatever.</font><font color="#008080" size="2">BaseS</font><font size="2"><</font><font color="#008080" size="2">Kappa</font><font size="2">></font>

    [/quote]

    This line makes total sense, it's just that your second use of <font color="#008080">Kappa</font><font size="2"> may not be doing what you "think" it should be doing</font>. That doesn't mean it isn't perfectly legitimate. I'll start with - It does not seem like generics is the appropriate solution for your problem...especially if you do not know how it works. Just because something is available doesn't mean you should use it. :)

    You should check out Generics (C# Programming Guide). There is plenty of information on generics there. Specifically, you may want to look at Introduction to Generics (C# Programming Guide) in order to get the basics of generics.

    With that said, the reason this works is your <font color="#008080">Kappa</font><font size="2"> class is derived from your <font color="#008080">BaseS</font> class. Your <font color="#008080">BaseS</font> class is a generic class who's generic type is defined as a <font color="#008080">Kappa</font> type. Remember:</font>

    Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.

    All this means is that the object you chose not to type (to be generic) in your <font color="#008080">BaseS</font> class is going to be defined, and strongly typed, as a <font color="#008080">Kappa</font><font size="2"> class. I will use a very simple example to explain this. So, if it does not fit your specific situation, do not brush it off. Read the materials listed above. :)</font>

    <font size="2">So let's say you have the following:</font>

    public class MyBase<T>
    {
    public T ExposedField;
    }

    public class MyChild : MyBase<MyChild>
    {
    public Guid Id;
    }

    What will happen when you instantiate a <font color="#2b91af">MyChild</font>? It will have two public fields: an Id of type Guid and an ExposedField of type MyChild. There is nothing strange about this and it is working as intended. In fact, the above example will compile and work perfectly fine (if you ever needed a class with an id that was capable of containing something of its own type). This is a very poor use of generics; however, it does work, compile, and perform as intended. Cut/paste it in your code, instantiate a <font color="#2b91af">MyChild</font> and take a look at it in the debugger. For a proper use of generics, check out the sample located here.

    Please do not take any of this as a flame, bash, etc. This post was not intended to upset you in any way. I was honestly trying to be helpful. If you'd like to post more information on the problem you are trying to solve, I'd be happy to see if I have a suggestion. With the information posted, it's honestly hard to offer anything beyond "working as intended." :) If there is a specific need for generics, reflection, etc. please let me know, I am happy to offer any advice I can. Again, please know this was posted with the best of intentions. :)



  • [quote user="thisguy"]<font size="2">So let's say you have the following:</font>

    public class MyBase<T>
    {
    public T ExposedField;
    }

    public class MyChild : MyBase<MyChild>
    {
    public Guid Id;
    }

    What will happen when you instantiate a <font color="#2b91af">MyChild</font>? It will have two public fields: an Id of type Guid and an ExposedField of type MyChild. There is nothing strange about this and it is working as intended. In fact, the above example will compile and work perfectly fine (if you ever needed a class with an id that was capable of containing something of its own type). This is a very poor use of generics; however, it does work, compile, and perform as intended.[/quote]

    Why do you say this is a very poor use of generics?  It looks perfectly fine to me.



  • Isn't any less different then the IComparable<T> interface

    public class Kappa : IComparable<Kappa>

     Nothing wrong with that, I use it all the time.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.