Question: I hope you can help me with the following, i have a php method to scan a directory with pictures: 

```
<?php
                        $dirname = "images/";
                        $images = scandir($dirname);
                        $ignore = Array(".", "..");
                        foreach($images as $curimg){
                            if(!in_array($curimg, $ignore)) {

                            echo "<li><a href=\"$dirname$curimg\"> class=\"portfolio-item-preview\" <img src=\"img/portfolio/portfolio-img-01.jpg\" /></a></li>\n ";

                            // echo "<li><a href=\"$dirname$curimg\"><img src=\"img.php?src=$dirname$curimg&w=300&zc=1\" alt='' /></a></li>\n ";
                                                }
                        }               
                    ?>

```

I want to put the following code after the foreach loop:

```
                            <li class="one-fourth logos">
                        <p>
                            <a href="images/project2/IMG_0268.jpg" class="portfolio-item-preview" data-rel="prettyPhoto"><img src="img/portfolio/portfolio-img-01.jpg" alt=" " width="210" height="145" class="portfolio-img pretty-box"/></a>
                        </p>
                        </li>

```

If I echo all the lines PHP generates all kinds of errors, could please someone help me with parsing the lines? I am just a beginner, sorry if it's very nooby:) I want this to create a class one-fourth logos for as long the folder contains images.

Answer: 
I ended up creating my own wrapper:

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

public sealed class ConditionalHashSet<T> where T : class
{
    private readonly object locker = new object();
    private readonly List<WeakReference> weakList = new List<WeakReference>();
    private readonly ConditionalWeakTable<T, WeakReference> weakDictionary =
        new ConditionalWeakTable<T, WeakReference>();

    public void Add(T item)
    {
        lock (this.locker)
        {
            var reference = new WeakReference(item);
            this.weakDictionary.Add(item, reference);
            this.weakList.Add(reference);
            this.Shrink();
        }
    }

    public void Remove(T item)
    {
        lock (this.locker)
        {
            WeakReference reference;

            if (this.weakDictionary.TryGetValue(item, out reference))
            {
                reference.Target = null;
                this.weakDictionary.Remove(item);
            }
        }
    }

    public T[] ToArray()
    {
        lock (this.locker)
        {
            return (
                from weakReference in this.weakList
                let item = (T)weakReference.Target
                where item != null
                select item)
                .ToArray();
        }
    }

    private void Shrink()
    {
        // This method prevents the List<T> from growing indefinitely, but 
        // might also cause  a performance problem in some cases.
        if (this.weakList.Capacity == this.weakList.Count)
        {
            this.weakList.RemoveAll(weak => !weak.IsAlive);
        }
    }
}

```
In some recent framework version, the `ConditionalWeakTable<TKey,TValue>` now implements `IEnumerator` interface. Check out [Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.conditionalweaktable-2.system-collections-ienumerable-getenumerator?view=netcore-3.1).

This applies to 

* *.NET Core* >= `2.0`
* *.NET Standard* >= `2.1`

This is not solving the problem if someone is stuck with *.NET Framework*. Otherwise, this may help if, like me, it's only a matter of updating from *.NET Standard* `2.0` to `2.1`.