Getting a ref/address from a list in C#

I am aware that C# does not deal with pointers but I wonder if I can get a ref to a list when I know its first element? For example: Let's say I have a list defined as

List<T> abc  

If I have abc[0], can I get a reference of abc? I am new to C#, I apologize if my question seems weird. In C/C++, I can get the address of an array abc by using &abc[0]. Does C# provide us with similar tool that help us refer back to the collection itself when we know one item in the collection? Thanks,


Collections don't work the same way in C# as they do in C++, for example you can add the same object to multiple different collections and so it doesn't really make sense to ask to get a reference to the list that an object is contained in, as it could be in many lists (or none, or even in the same list multiple times)

object myObject = new object();

List<object> list = new List<object>();

object[] someArray = new object[] { myObject  };
Assert.AreEqual(list[0], someArray[0]);

If it helps you can think of lists in C# as being lists of pointers references to the objects being stored where the pointer itself is hidden from you, although understand that in reality the implementation may be more complicated (and is also irrelevant).

If there is a relationship between the objects in a list and the list contents of that list then its up to you to explicitly declare and keep track of what that realtionsip is, for example through a Parent property on the object in the list

List<T> myList = new List<T>();

// Whenever an item is added to myList set the Parent property
item.Parent = myList;

This is what Windows Forms does in order to maintain the relationship between the controls in a container, and the container in which those controls are contained. Obviously you should decide what to do if someone tries to add the same object to multiple lists.

Need Your Help

Change XMPPPresence to Away/Busy/Invisible

ios objective-c xmpp xmppframework user-presence

How do you change your presence to show dnd/away and etc.?

windows git https clone through proxy server failed to write files

windows https github clone proxy-server

In the process of cloning a repo through my company proxy server through sysinternal I can see the it tried to write the git repository but failed. However, the .git directory was created but mostl...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.