Impossible Implicityly Converting a Void Type to Input System.Collections.Generic.IList <T>

Trying to return the generic type and get the error described in the header. I'm sure I'm doing something stupid - the tips are appreciated ...

public static IList<T> GetGroupById<T>(int groupId)
        {

            DashboardGroupType type = (DashboardGroupType)groupId;
            IList<T> result = null;

            var obj = default(T);

            switch (type)
            {
                case DashboardGroupType.Countries:
                    break;
                case DashboardGroupType.Customers:
                    // this returns a list of typ  IEnumerable<Customer>
                    obj = (T) CustomerRepository.GetAllCustomers();
                    break;
                case DashboardGroupType.Facilities:
                    // this returns a list of typ  IEnumerable<Facility>
                    obj = (T) FacilityRepository.GetAllFacilities();
                    break;
                case DashboardGroupType.Heiarchy:
                    break;
                case DashboardGroupType.Lines:
                    break;
                case DashboardGroupType.Regions:
                    // this returns a list of typ  IEnumerable<string>
                    obj = (T) CustomerRepository.GetRegionsHavingCustomers();
                    break;
                case DashboardGroupType.States:
                    // // this returns a list of typ  IEnumerable<Customer>
                    obj = (T) CustomerRepository.GetStatesHavingCustomers();
                    break;
                case DashboardGroupType.Tanks:
                    break;
                default:
                    break;
            }

            result = result.Add(obj); // ERROR IS THROWN HERE

        }
+3
source share
6 answers

Add method returns nothing. He just changes the list. That is why you get an error. Just remove the assignment:

result.Add(obj);

Another problem is that you are not initializing the result. When you run the code, you will get a NullReferenceException. You need something like this:

IList<T> result = new List<T>();

You will also need to return a value from this function. I guess you want

return result;

CustomerRepository.GetAllCustomers(); FacilityRepository.GetAllFacilities(); IEnumerable<Customer> IEnumerable<Facility> . T. , T.

, . , IEnumerable<T> AddRange.

, . , , / .

+8
result = result.Add(obj);

() . "result ="

+10

, IList, (, ). , .

EDIT:

, , , result.Add void,

result = result.Add(obj); ( List). , , - .

+4

result = result.Add(obj);

,

result.Add(obj);

- return result;

+3

: , , null.

+1
source

You do not have a return statement.

return result;
+1
source

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


All Articles