Answer the question
In order to leave comments, you need to log in
Why does it return null when using a generic class object method?
There is a generic class that describes repositories. Here is the code
public class BaseServices<T> : IBaseService<T> where T : BaseEntity, new()
{
public List<T> AllItem = new List<T>();
public bool Delete(int id)
{
bool b = false;
foreach (var a in AllItem)
{
if (a.Id == id)
{
AllItem.Remove(a);
b = true;
}
}
return b;
}
public T Get(int id)
{
T b = null;
foreach (var a in AllItem)
{
if (a.Id == id)
{
b = a;
return b;
}
}
return b;
}
public object GetAll()
{
object b = null;
foreach (var a in AllItem)
{
b = a;
return b;
}
return b;
}
public bool Save(T entity)
{
entity = new T();
AllItem.Add(entity);
return true;
}
}
List<AccountModel> Accounts = new List<AccountModel>();
private BaseServices<AccountModel> AccountBS = new BaseServices<AccountModel>();
Random rnd = new Random();
public void Repletion()
{
for (int index = 0; index < 100; ++index)
{
AccountBS.AllItem[index] = new AccountModel();
AccountBS.AllItem[index].Id = index;
}
}
[TestMethod]
public void GetTest()
{
int SomeId = rnd.Next(100);
AccountModel result = AccountBS.Get(SomeId);
Assert.IsNotNull(result);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question