Question to ElementAt()

c#chris

Active member
Joined
Dec 10, 2024
Messages
32
Programming Experience
1-3
If I have a list like:

C#:
Snippetprivate List<SetupInfo> _sourceBarListLong { get; set;} = new ();

I can use var obj = _sourceBarListLong.ElementAt(abc) to get the Object obj of type SetupInfo at Index abc;

If I change the derived object from the list, e.g. obj.Direction = Right;

Do I habe to write the changed object back to the list or did I change it within the list ?

My understanding is, that I have to write it back: _sourceBarListLong[abc] = obj;
 
If SetupInfo is a reference type, then you do not have to write it back into the list. If it is a value type then you do. How did you declare SetupInfo ?
 
SetupInfo is defined as follows:

C#:
internal class SetupInfo
{
      //fields
    //properties
   
    //ctor
   
    //private methods
   
    //public methods
}

Objects are stored as folows in the list mentioned above:

var obj = new SetupInfo();
_sourceBarListLong.Add(obj);

What I need to do is to change some public properties of the obj's at specific indexes of the list.
 
Yes. But what kind of object is SetupInfo? Is it:
C#:
class SetupInfo
or
C#:
struct SetupInfo

That will determine whether it is a reference type or value type.

Since you come from a C/C++ background, think of it this way:
C++:
class SetupInfo
{
    :
};

typedef SetupInfo* PSetupInfo;
PSetupInfo rgpsetupinfo = new PSetupInfo[25];    // this is what happens in a list of C# reference types
SetupInfo rgsetupinfo = new SetupInfo[25];     // this is what happens in a list of C# value types

// or if you use the standard library:
std::list<PSetupInfo> lstpsetupinfo;    // this is what happens in a list of C# reference types
std::list<SetupInfo> lstsetupinfo;     // this is what happens in a list of C# value types
 
Last edited:
Sorry. My phone didn't show your code block I post #3 when I first viewed it.

Anyway, since it's declared as a class, it will be a reference type, and so you don't have to put the obj back into the list because ElementAt() will give you back a that is the thing at the particular index. Since that thing there is a reference, you'll also get a reference.
 
Back
Top Bottom