C# Generic methods / Polymorphism

Viewed 126

I have the following code that I use to close a stream.

void CloseStream(Stream s)
{
    if (s != null)
        s.Close();
}

void CloseStream(HttpWebResponse s)
{
    if (s != null)
        s.Close();
}

void CloseStream(StreamReader s)
{
    if (s != null)
        s.Close();
}

The code works perfectly but I would like to refactor the 3 methods if it's possible. Ideally the method would look like that:

void CloseStream(Object obj)
{
    obj.Close();
}

But I cannot do obj.Close() because the Object class does not implements such a method. So I was wondering if any of you had any idea how to resolve it.

Thanks for your time, Kevin

3 Answers
Related