Question: I'm making a game using Monogame, and I've been trying to figure out how to implement a function that acts similarly to AS3's and GML's with statement.

So far I have a system that works, but not entirely the way I want it to. I store my GameObjects in a Dictionary of Lists. This is so I can get to the specific type of object I want to access without having to loop through a list of ALL objects. The key used is the name of the type.

```
public static Dictionary<string, List<GameObject>> All = 
    new Dictionary<string, List<GameObject>>();

```

I access all of a specific type of object using AllOf. If a List containing that type exists in the Dictionary, it returns that List, else it returns an empty list.

```
public static List<GameObject> AllOf(Type type)
{
    string key = type.Name;

    if(All.ContainsKey(key))
    {
        return All[key];
    }

    return new List<GameObject>();
}

```

An example of how these are implemented

```
public override void Update(GameTime gameTime)
{
    List<GameObject> list = Instance.AllOf(typeof(Dummy));

    for(int i = 0; i < list.Count; i++)
    {
        list[i].Update(gameTime);
        list[i].foo += bar;
    }
}

```

But I'd rather use something similar to the AS3/GML with statement, which would also allow for other, non-member codes to be executed.

```
with(typeof(Dummy))
{
    Update(gameTime);
    foo += bar;

    int fooBar = 2;
    someObject.someMemberFunction(fooBar);
}

```

Is there a way to accomplish this? My end goal is just to make my code look a little cleaner, and make it easier to make a lot of changes without having to type out a for loop each time.

Answer: 
No such syntax exists in C#, but you *can* access methods within the `for` that have nothing to do with the collection:

```
public override void Update(GameTime gameTime)
{
    List<GameObject> list = Instance.AllOf(typeof(Dummy));

    for(int i = 0; i < list.Count; i++)
    {
        list[i].Update(gameTime);
        list[i].foo += bar;

        int fooBar = 2;
        someObject.someMemberFunction(fooBar);
    }
}

```

Note that you can also use [`foreach`](https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx?f=255&MSPPError=-2147217396), which is a *little* cleaner if you don't need the indexer:

```
foreach(var item in list)
{
    item.Update(gameTime);
    item.foo += bar;

    int fooBar = 2;
    someObject.someMemberFunction(fooBar);
}

```
try

```
using(Object myObject = new Object()){

}

```

i think this might be what your looking to use?