-1

Ok, I know from documentation (from here and here) how to write simple generic classes with generic methods. But in my case I have something different and I'm not sure what is the right way to implement this. So this is what I have in semi pseudo-code

// This is a base class
public class Parent_Class<T>{
   T field_1;
   public void method_1<T2>(){
       T2 local_variable = new T2();
   }
}
// ^^^ Is it right?? And is it possible to inherit from this class in this way (option 1):

public Child_Class : Parent_Class <Class_1>{
    public void method_1(){
        base.method_1<Class_2>();
        // ^^^ will this work???
    }
}

And I see another alternative which may look in semi pseudo-code like this:

// This is a base class
public class Parent_Class<T, T2>{
   T field_1;
   public void method_1<T2>(){
       T2 local_variable = new T2();
   }
   // ^^^ should it be declared like public void method_1<T2>
   // or can we simply declare it with public void method_1(){
}
// ^^^ Is it right?? And is it possible to inherit from this class in this way (option 1):

public Child_Class : Parent_Class <Class_1, Class_2>{
    public void method_1(){
        base.method_1<Class_2>();
        // ^^^ should I call it like this? or can I do simply 
        //base.method_1();
    }
}

So, as you can see there is a mixture of generic types. I put my questions in the code and hope someone can point me to the right direction. Thanks!

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • 5
    The code look plausible (and may even compile), but you did not said what you actually want to achieve... or why you expect this code to fail/not do what you want. – Alexei Levenkov Nov 07 '15 at 07:07
  • @Alexei Levenkov. Since I see two ways of creating a Parent class and inheriting from it and I see various options of how to call the Parent method from the Child class, I'm not sure what combination will work, is better and used in practice. – Jacobian Nov 07 '15 at 07:09
  • Both options will work. However, a type listed as generic parameter usually appears either in the signature of the generic method or as a return type. What you are trying to do can be easily achieved without generics at all. – alexm Nov 07 '15 at 07:14
  • 2
    `C` and `C` approaches are different and serve different needs - how do you expect SO to decide which one would work for *your* case? Both variants are good and used in practice. – Alexei Levenkov Nov 07 '15 at 07:15
  • @Alexei. Can you please elaborate a bit on this in the format of answer - to explain why they serve different needs and why one way is preferable to another in different contexts? I will definitely accept this answer! – Jacobian Nov 07 '15 at 07:17
  • 1
    You write two sets of base-derived class yourself (actual C# code, not pseudo code) in the two formats `BaseClass` & `BaseClass`, you'll understand most of the differences yourself. – Arghya C Nov 07 '15 at 07:23
  • @Jacobian Though you are showing only pseudo-code but I can see some wrong code intention. For e.g: you can't new up a generic type unless you specify default constructor constraint on that type or you need to use `default(T)`. If you use Generic-Type argument across the class without new Generic-Types defined on the methods, then your contract is clear that all the methods in the class will work with declaring type only. I suggest you once again go through this [MSDN](https://msdn.microsoft.com/en-us/library/d5x73970.aspx) – Siva Gopal Nov 07 '15 at 07:33

1 Answers1

1

Since the problem has reduced to passing args to template type constructor..

using System;

class A
{
    public A() { }
}  
class B
{
    private int data;
    public B() { }
    public B(int x) { data = x; }
    public int Data { get { return data; } }
}
class Base<T1, T2> where T2 : new()
{
    private T1 a;
    public void Method(object[] args)
    {
        //pass args to T2 constructor 
        T2 b = (T2)Activator.CreateInstance(typeof(T2), args);
    }
}
class Derived : Base<A,B>
{
    public new void Method(object[] args)
    {
        base.Method(args); 
    }
}
class Test
{
    static void Main()
    {
        Derived d = new Derived();
        d.Method(new object[]{ 1 });
    }
}
Acha Bill
  • 1,255
  • 1
  • 7
  • 20
  • Thank you, sir! But can you please touch a more general case when instead of standard parameters like `char `or `int` in your example we use custom/user defined classes. I'm asking this, because I'm not sure how to transform this `T2 y = default(T2);` to something like `T2 y = new T2(param1, param2)` - I tried it hard, but was not able to implement this. – Jacobian Nov 07 '15 at 07:55
  • `T2 y = new T2(param 1,param 2)` will work if T2 has `new () ` constraint – Acha Bill Nov 07 '15 at 08:02
  • I added `new()` constraint, but get an error message, that `TestClass must have a public parameterless constructor ....`. So, I'm not able to so something like `T2 y = new T2(1)`. To add to it coustructor of `TestClass` in my case has parameters: `public TestClass(int a){...`. So, I'm not sure how to tackle this – Jacobian Nov 07 '15 at 08:06
  • So, when a custom/user defined class has a constructor with no parameters, then `T2 y = new T2()` works (indeed we add `new()` constraint to do that). But if the constructor has parameters, then a lot of errors pop up. – Jacobian Nov 07 '15 at 08:09
  • http://stackoverflow.com/questions/840261/passing-arguments-to-c-sharp-generic-new-of-templated-type will solve your problem – Acha Bill Nov 07 '15 at 09:05