Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public IEnumerable<Role> GetRoles()
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mystring"].ToString()))
            {
                return con.Query<Role>("Usp_GetRoles", null, null, true, 0, CommandType.StoredProcedure).ToList();
            }
        }


What I have tried:

I want to know why define this type please guys help me
Posted
Updated 12-Jul-16 8:25am

1 solution

The short answer is because it makes the function even more of a black box.

The caller doesn't need to know or even care if you have implemented the retrieval as a Role[] or a List<role> or any other collection type... provided the collection implements the interface IEnumerable

So if you found a newer and far more efficient way of retrieving the data that returned a different sort of collection implementing IEnumerable<> you could change the guts of the method and apart from the reduction in call time it wouldn't affect any existing code.

If, say, you had made the return type Role[] and you changed the guts to return List<role> then every call of the method would have to be changed.

USe your favourite search engine to look up "programming to an interface". There are lots of articles that explain the idea. Here's just one...

Program to an interface not an implementation/[^]
 
Share this answer
 
v2
Comments
Member 10874581 12-Jul-16 14:56pm    
not clear
Ben J. Boyle 12-Jul-16 15:29pm    
What's not clear? The developer clearly chose to return a more flexible type than forcing a list or an array. As @cigwork says this would allow the internals of the function to change without having to alter any code that uses the results of the function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900