Below is my structure in c#
public class Search
{
public IEnumerable<Cars> Cardetail { get; set; }
}
public class Cars
{
public string Id{ get; set; }
public string StatusCode { get; set; }
}
Now I want to assign data to the Cars.Id and Cars.Status code for unit testig
How to assign it since it is part of the IEnumerbale
I tried like
new Search()
{
Cardetail = <IEnumerable> Cars{ Id ="1"} // but throws error
}
Thanks in advance
var search = new Search
{
Cardetail = new Car[]
{
new Car
{
Id = "UniqueId",
StatusCode = "MyFancyStatusCode"
}
}
}
This code should probably do what you're trying to achieve.
When creating an instance of a class with the new
keyword, you can use Object and Collection Initializers.
Instead of writing this
var search = new Search();
search.Cardetail = ...
we assign Cardetail
in the curly braces.
For Cardetail
we have to decide for an implementation to use, since Cardetail
is of type IEnumerable<T>
which is an Interface and we cannot create instances of interfaces. We need a type which implements this specific interface like Array
or List<T>
(there are many more).