Is there a better way to achieve what I am doing?

I'm new to C # and coding, so forgive my ignorance. I tried to research, and it looks like I'm trying to find, perhaps something called a "pointer" in C ++, but not available in C #? I want a field or property that I can assign to an object from the list as a "selected object" and play until I "let it go." I tried this:

    Entity GrabbedEntity;

    List<Entity> Entities;

    if (mouseDown)
        GrabbedEntity = Entities[3];

    if (mouseUp)
        GrabbedEntity = null;

What I'm trying to achieve basically just frees up the object that I assigned "grabbedEntity" and actually don't make "Entities [3]" null, I still want to be able to change the grabbedEntity values ​​though (mouseDown and = Entities [ 3] for simplicity, it can be any object in the list, depending on the circumstances). How I worked on this:

List<Entity> Entities;

List<Entity> _grabbedEntityList = new List<Entity>();
Entity GrabbedEntity
{
    get 
    { 
        if (_grabbedEntityList.Count < 1) return null; 
        else return _grabbedEntityList[0]; 
    }
    set
    {
        if (value == null) _grabbedEntityList.Clear(); 
        else 
            _grabbedEntityList.Clear(); 
            _grabbedEntityList.Add(value);
        }
    }
}

, , "GrabbedEntity" "Entities", , , , , , null, , -, . .

+4
1

.

: " , :", , , , . , GrabbedEntity = null; Entities[3]; null, , , , , .

, get{} set{}, , , .

+4

Source: https://habr.com/ru/post/1656104/


All Articles