.NET 4.x Переменная меняется при передаче ссылки на нее - C#
Формулировка задачи:
Здравствуйте. При передаче переменной FindCards:
ListInventory:
list
в методFindCards
и записи результата работы метода в переменнуюcards
, переменнаяlist
тоже меняется и становится равнаcards
. Как вы понимаете, я так не задумывал. Почему меняется переменнаяlist
? Код:ListInventory list = await Steam.GetInventory("/753/6/"); //list.rgDescriptions.Count == 5
ListInventory cards = Tools.FindCards(list); //list.rgDescriptions.Count == 1, cards.rgDescriptions.Count == 1
if (list == cards) //true
{}
public static ListInventory FindCards(ListInventory inventory)
{
var list = new ListInventory();
list = inventory;
for (int i = 0; i < inventory.rgDescriptions.Count; i++)
{
if (inventory.rgDescriptions[i].type.Contains("Trading Card") == false)
{
list.rgInventory.Remove(inventory.rgInventory[i]);
list.rgDescriptions.Remove(inventory.rgDescriptions[i]);
i--;
}
}
return list;
} public class ListInventory
{
public bool seccess { get; set; }
public List<ListInventoryItem> rgInventory { get; set; }
public List<object> rgCurrency { get; set; }
public List<ListDescriptionItem> rgDescriptions { get; set; }
public bool more { get; set; }
public bool more_start { get; set; }
}Решение задачи: «.NET 4.x Переменная меняется при передаче ссылки на нее»
textual
Листинг программы
public ListInventory Clone()
{
return new ListInventory
{
seccess = this.seccess,
rgDescriptions = this.rgDescriptions.Select(rgDescriptions => rgDescriptions).ToList()
};
}