Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can this C# generic code be simplified and just one type be passed in?

Tags:

c#

c#-3.0

c#-4.0

The following code from my team mate works:

 public T Get<T, V>(V repo, string pk, string rk)
            where T : Microsoft.WindowsAzure.StorageClient.TableServiceEntity
            where V : IAzureTable<T>
        {
            try
            {
                var item = repo.GetPkRk(pk, rk);
                if (item == null) throw new Exception(); return (T)item;
            }
            catch (Exception ex)
            {
                _ex.Errors.Add("", typeof(T).Name + rk + " does not exist");
                throw _ex;
            }

        }

Calling code:

var account = base.Get<Account, IAzureTable<Account>>(_accountRepository, pk, rk);

Can it be simplified. The only type variable here is "Account" and I am wondering if the T and V types could be combined into one as V depends only on T.

like image 229
Samantha J T Star Avatar asked Dec 28 '22 09:12

Samantha J T Star


1 Answers

Since the only place that V appears is in the signature, I'd have thought you could change it to:

public T Get<T>(IAzureTable<T> repo, string pk, string rk)
        where T : Microsoft.WindowsAzure.StorageClient.TableServiceEntity

But I don't have sample code handy to test this.

like image 190
Damien_The_Unbeliever Avatar answered Jan 31 '23 14:01

Damien_The_Unbeliever