standards-c-sharp

Collections types for public APIs

We define a public API as:

Input parameters for public APIs

Don’t

public class MyPublicService
{
    public string DoWork(List<string> ss)
    {
        // we do not use any of List<T>'s methods...    
        return ss.FirstOrDefault()?.ToUpper() ?? "";
    }
}

Do

public class MyPublicService
{
    // ...so let's accept an IEnumerable<T> instead to be nice to the client
    public string DoWork(IEnumerable<string> ss)
    {   
        return ss.FirstOrDefault()?.ToUpper() ?? "";
    }

Return types for public APIs

Don’t

public class MyDto
{
    public List<string> MyStrings;
}

Do

public class MyDto
{
    // now we are free to change the implementation to 
    // almost anything without breaking the client
    public IEnumerable<string> MyStrings;
}