Creating custom objects with custom properties using generics I have two base classes: City and Building:

public class City
{
    public string Name { get; set; }
    public double Area { get; set; }
}

public class Building
{
    public string Name { get; set; }
    public double Area { get; set; }
    public int Stories { get; set; }
}


With two classes that inherit from Building, with their own Id property:

public class MyBuilding : Building
{
    public int MyId { get; set; }
}

public class HisBuilding : Building
{
    public int HisId { get; set; }
}


And two classes that inherit from City, with their own Id and IEnumerable&lt;Building&gt; properties:

public class MyCity : City
{
    public int MyId { get; set; }
    public IEnumerable&lt;MyBuilding&gt; Buildings { get; set; }
}

public class HisCity : City
{
    public int HisId { get; set; }
    public IEnumerable&lt;HisBuilding&gt; Buildings { get; set; }
}


Below is a method I've created that returns a KeyValuePair&lt;City, Building&gt; using generics:

public static KeyValuePair&lt;TCity, IEnumerable&lt;TBuilding&gt;&gt; GetData&lt;TCity, TBuilding&gt;()
    where TCity : City, new()
    where TBuilding : Building, new()
{
    TCity city = new TCity();
    IEnumerable&lt;TBuilding&gt; buildings = new List&lt;TBuilding&gt;();
    return new KeyValuePair&lt;TCity, IEnumerable&lt;TBuilding&gt;&gt;(city, buildings);
}


And in my Main method I've logic that instantiates a specific City object based on a user's input.

int input = -1;
string strInput = Console.ReadLine();
int.TryParse(strInput, out input);
string json = string.Empty;
switch (input)
{
    case 1:
        KeyValuePair&lt;MyCity, IEnumerable&lt;MyBuilding&gt;&gt; myCity
            = GetData&lt;MyCity, MyBuilding&gt;();
        MyCity my = myCity.Key;
        my.MyId = 1;
        my.Buildings = myCity.Value;
        json = JsonConvert.SerializeObject(my, Formatting.Indented);
        break;

    default:
        KeyValuePair&lt;HisCity, IEnumerable&lt;HisBuilding&gt;&gt; hisCity
            = GetData&lt;HisCity, HisBuilding&gt;();
        HisCity his = hisCity.Key;
        his.HisId = 2;
        his.Buildings = hisCity.Value;
        json = JsonConvert.SerializeObject(his, Formatting.Indented);
        break;
}
Console.WriteLine(json);
Console.ReadLine();


The above generic method is just something I cobbled together in the last half hour, but I'm wondering if there is a more efficient way of creating these custom objects of mine, especially when each City has its own, different IEnumerable&lt;Building&gt; property.

EDIT

Here is the code I have for building the IEnumerable inside my GetData method:

public static IEnumerable&lt;TBuilding&gt; GetBuildingData&lt;TBuilding&gt;() 
    where TBuilding : Building, new()
{
    DataTable table;
    string myConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDb;Initial Catalog=Test;Integrated Security=True;";
    List&lt;TBuilding&gt; buildings = new List&lt;TBuilding&gt;();
    using (SqlConnection connection = new SqlConnection(myConnectionString))
    {
        using (SqlCommand command = new SqlCommand("[dbo].[GetListData]", connection) { CommandType = CommandType.StoredProcedure })
        {
            connection.Open();
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                table = new DataTable();
                adapter.Fill(table);
            }
            connection.Close();
        }
    }
    foreach (DataRow row in table.Rows)
    {
        TBuilding building = new TBuilding();
        building.Area = Convert.ToDouble(row["Area"]);
        building.Name = row["Name"].ToString();
        building.Stories = Convert.ToInt32(row["Stories"]);
        buildings.Add(building);
    }
    return buildings;
}
I will focus on..


  Creating custom objects with custom properties using generics  


Which you do in this method..


public static KeyValuePair&lt;TCity, IEnumerable&lt;TBuilding&gt;&gt; GetData&lt;TCity, TBuilding&gt;()
    where TCity : City, new()
    where TBuilding : Building, new()
{
    TCity city = new TCity();
    IEnumerable&lt;TBuilding&gt; buildings = new List&lt;TBuilding&gt;();
    return new KeyValuePair&lt;TCity, IEnumerable&lt;TBuilding&gt;&gt;(city, buildings);
}




A KeyValuePair is archaic, you can use a ValueTuple nowadays
TBuilding does not require the new() constraint in this method
The choice for IEnumerable over IList is questionable, since you retrieve the items later on and have no way of adding them to an IEnumerable. So why create the IEnumerable in this method?
The name should reflect what you are doing. CreateCity seems more appropriate.


I would refactor this method..

public static (TCity city, IList&lt;TBuilding&gt; buildings) CreateCity&lt;TCity, TBuilding&gt;()
    where TCity : City, new()
    where TBuilding : Building
{
    var city = new TCity();
    var buildings = new List&lt;TBuilding&gt;();
    return (city, buildings);
}


EDIT:

Thinking about the refactored method. This doesn't even need to be a method anymore. It doesn't do anything but create objects for the types you specify.

What I would really do is throw out this ancient way of ORM, and use an existing API (EF, NHibernate, Dapper).