How to remove variable of Specific Type from Object[] in C#

How to remove variable of Specific Type from Object in C#



I have an array of Objects type which have information of variable of different classes, say, ClassA and ClassB.


Objects


ClassA


ClassB


public object SelectedObjects get;



Now I am filtering the objects of ClassB which I need to remove from the array of SelectedObjects.


ClassB


SelectedObjects


var selectedClassBObjects = SelectedObjects.OfType<ClassB>().ToList();



When I perform the operation of Remove() or RemoveAll(), it does not do anything.


Remove()


RemoveAll()



Can anyone suggest me how to perform this operation?



I need to remove objects of ClassB because at a time only one object of ClassB can be present when user is trying to Highlight the objects on the canvas. First remove the ClassB objects then add the newly selected ClassB objects to SelectedObjects.


ClassB


ClassB


ClassB


ClassB


SelectedObjects





if you want to modify the collection, array is not a good choice; try List<T>: public List<object> SelectedObjects get;; then SelectedObjects.RemoveAll(item => !(item is ClassB));
– Dmitry Bychenko
Sep 3 at 9:23



List<T>


public List<object> SelectedObjects get;


SelectedObjects.RemoveAll(item => !(item is ClassB));





maybe this will help you: stackoverflow.com/questions/496896/…
– AsfK
Sep 3 at 9:24




2 Answers
2



You can't remove anything from an array, you will need to create a new one. If you want to exclude objects of type ClassB you can do:


ClassB


SelectedObjects = SelectedObjects.Where(x => !(x is ClassB)).ToArray();



If you want a modifiable collection, List<T> is a better choice than an array.


List<T>





SelectedObjects get; is read only property; SelectedObjects = probably (if it's not in the constructor) will not compile
– Dmitry Bychenko
Sep 3 at 9:26



SelectedObjects get;


SelectedObjects =





yeah well, do I have to say everything?
– Selman Genç
Sep 3 at 9:26



If you insist on array you can put the backing field explicitly:


private object m_SelectedObjects = new object[0];

public object SelectedObjects
get
return m_SelectedObjects;



private void MyRemove()
// We can't modify read-only property but can operate with its backing field
m_SelectedObjects = m_SelectedObjects?.OfType<ClassB>()?.ToArray();



Another possibility is to change array object into List<object>:


object


List<object>


public List<object> SelectedObjects get;

...

// We still can't assign SelectedObjects but we can modify the collection now
SelectedObjects?.RemoveAll(item => !(item is TypeB));



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)