Generic List<> as a property

I have a Type List<string>. How would I call the clear method on the List if it was a property.
This is how my property looks
List<string> _RecieveData = new List<string>();//Data Returned from the clock
        public List<string> RecieveData
        {
            get
            {
                return _RecieveData;
            }
            set
            {
                _RecieveData.Clear();
            }
        }

Open in new window

u2envy1Asked:
Who is Participating?
 
u2envy1Connect With a Mentor Author Commented:
Thx.....
Are Generics Ref types or Value types ?
0
 
OBonioConnect With a Mentor Commented:

<object>.ReceiveData.Clear()

Open in new window

0
 
AcumentalityCommented:
If you want to use the setter to clear, you could use RecieveData = null; Your set method would then ignore the nulll (because you haven't used value). But I don't know why you would do it like that, that's just confusing.

RecieveData.clear() is more clear.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
u2envy1Author Commented:
If I assign another List<string> MyList = RecieveDate;
Then run  RecieveData.clear()
The MyList is also Cleared. Due to Reference types.
How do I not clear MyList when clearing RecieveDate ?
0
 
AcumentalityCommented:
They both refer to the same object. If you clear one, you clear both.
You'd have to create another list. If you want to copy the elements from recievedata to that list at the same time, use:
List<String> myList = new List<String>(RecieveData);

Open in new window

0
 
käµfm³d 👽Connect With a Mentor Commented:
Have you tried changing your get to:
get
{
    return new List<string>(_RecieveData);
}

Open in new window

0
 
u2envy1Author Commented:
Thanks this works......
get
{
    return new List<string>(_RecieveData);
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.