0

What i need. I have a list of objects(list of icons). This data contain duplicate objects(since the same icons is used multiple times same property will repeat).so the ids of multiple object might be same. I need to loop this list with foreach and assign a unique id (named uniquid ) property to each object and add to a list.

problem when I try to assign uniqueId to each object, duplicate objects will update with same unique id. How can i restrict this? i need all the objects should have a unique value in uniqueid property

List<icons> icons = new List<icons>();
foreach (var single in fullList)
{   
     Guid id = Guid.NewGuid();
    single.uniquid = id;
    icons.Add(single);
}

here full list is also the List<icons>

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
developer
  • 11
  • 2
  • What is fullList ? – kuldeep Sep 02 '21 at 04:54
  • @kuldeep fulllist is List – developer Sep 02 '21 at 05:07
  • I do not get your problem, you said "duplicate objects will update with same unique id." -- but to me looks like each of the object of your fullList will get a unique Id, isnt this what you want ? – kuldeep Sep 02 '21 at 05:09
  • 1
    [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) - I'm going to guess that you have added the same instance of a refence type (i.e. a class) to the list twice. – ProgrammingLlama Sep 02 '21 at 05:15
  • @kuldeep fullList contain duplicate objects. but i need to differentiate this. so i added a new property called UniqueID. when i assign uniqueID inside foreach loop,uniqueID is mutating, and duplicate objects have same uniqueid. – developer Sep 02 '21 at 05:21
  • can you add the class definition of `icon` ? – SRIDHARAN Sep 02 '21 at 06:14

0 Answers0