The returned array 'this' in C #

I have a library that contains a class. This class is intended to be extended. It will connect to the database and generate class entries that extend it. Now I want to return an array of an expandable class type. How can i do this? For example:

public this[] search(string search) {
}

How to do it?

EDIT: I use generics, this is a generic class. This means that I have no clue as to which class that will expand mine will be. Therefore, I can’t just add the class name to [] ...

EDIT 2: As pointed out, returning an array from this makes no sense (my mistake then). So I want to return an array of an extended class type. For example:

public abstract class MappingObject {
    public ExtendedClassType[] search (string search) {
    }
}

Solution found._. I'm sorry it was very simple ...

public abstract class MappingObject<T> where T : new() {
    public static List<T> search(string search) {
    }
}
+4
3

, , - ( ):

public abstract class MyBaseClass<T> : where T : MyBaseClass<T>
{
    public abstract T[] Search(string search);
}

public class DerivedClass : MyBaseClass<DerivedClass>
{
    public override DerivedClass[] Search(string search)
    {
        return new DerivedClass[0];
    }
}

, , , , , , , .

, CSLA.NET, . , , " ", , . - , .


MS. , , . , , . -, , ( , , ). Eric:

, , , , #; , . , , , , , , , , .

- , #; , , ?

, , , API .

+4

'this' , : " MyClass", .

, : " "

, .

public YourClassName[] Search(string search)
{

}
+1

You mean

public class YourClass
{
    public T[] Search<T>(string search, Convertor<SearchResult, T> convertor)
    {
        ...
        List<SearchResult> results = ...
        return results.ConvertAll(convertor).ToArray();
    }
}

Thus, you delegate the transformation to the caller, who will know which type is passed as a parameter of the type type.

0
source

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


All Articles