Can generics be nested in a class definition

I am doing something like this:

public abstract class FolderNode<TChildNode,TChildBusObj> : Node
    where TChildNode : MappedNode<TChildBusObj>, new()
    where TChildBusObj : new()
{
..
}

Is there a way to exclude TChildBusObj from the definition, but still have access to it in the code? For instance. I want to be able to infer a generic type of a generic type. I tried this:

public abstract class FolderNode<TChildNode> : Node
    where TChildNode : MappedNode<TChildBusObj>, new()
{
..
}

but I get this compilation error:

CS0246: the name of the type or namespace "TChildBusObj" cannot be found (are you missing the using directive or assembly references?)

Update 22/01

I really decided that I want this:

public class Folder<TChildNode, TBusObj> : MappedNode<TBusObj[]>
    where TChildNode : MappedNode<TBusObj>, new()

which would be nice to shorten to this:

public class Folder<MappedNode<TBusObj>, TBusObj> : MappedNode<TBusObj[]>

But I can not, because I get this error:

CS0081: Type parameter declaration must be an identifier, not a type

I think you cannot anonymize nested generic types in a class definition

+3
4

TChildBusObj, , ?

+4

, .

0

  , , TChildBusObj.
  TChildBusObj, , - .
  TChildBusObj, , , TChildBusObj , , , , , , .
  , , , .

0

 public class Folder<TBusObj> : MappedNode<TBusObj[]>
 {
     void Add(MappedNode<TBusObj> newChild) { ... }
 }

?

0

Source: https://habr.com/ru/post/1729382/


All Articles