is it any point using the interface IDisposable like I do here

T

Tony Johansson

Hi!

Here I have a class named Team and this class implement the IDisposable
interface.
I just wonder is any point using the Dispose method on the data that I do
here.
The Dispose method is included at the end of the code.

[CLSCompliant(true)]
public class Team : IDisposable
{
private ArrayList teamList;
private int teamSize;
private bool disposedValue = false;

public int TeamSize
{
get { return teamSize; }
set
{
if (value <= 0)
throw new Exception("Invalid teamSize <" + value + ">Usage
teamSize > 0");
teamSize = value;
}
}

public Team(int teamCapacity)
{
teamList = new ArrayList(teamCapacity);
teamSize = teamCapacity;
}

public void Add(Player player)
{
try
{
if (teamSize > teamList.Count)
teamList.Add(player);
else
throw new TeamListFilledException("Team roster is full <" +
TeamSize + "> - Cannot add more Players");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public ArrayList Players
{
get { return teamList; }
}


#region IDisposable Members

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion

private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!disposedValue)
{
if (disposing)
{
teamList.Clear();
teamList = null;
}
disposedValue = true;
}
}
}
 
A

Arne Vajhøj

Here I have a class named Team and this class implement the IDisposable
interface.
I just wonder is any point using the Dispose method on the data that I do
here.
The Dispose method is included at the end of the code.

[CLSCompliant(true)]
public class Team : IDisposable
{
private ArrayList teamList;
private int teamSize;
private bool disposedValue = false;

public int TeamSize
{
get { return teamSize; }
set
{
if (value<= 0)
throw new Exception("Invalid teamSize<" + value +">Usage
teamSize> 0");
teamSize = value;
}
}

public Team(int teamCapacity)
{
teamList = new ArrayList(teamCapacity);
teamSize = teamCapacity;
}

public void Add(Player player)
{
try
{
if (teamSize> teamList.Count)
teamList.Add(player);
else
throw new TeamListFilledException("Team roster is full<" +
TeamSize + "> - Cannot add more Players");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public ArrayList Players
{
get { return teamList; }
}


#region IDisposable Members

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion

private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!disposedValue)
{
if (disposing)
{
teamList.Clear();
teamList = null;
}
disposedValue = true;
}
}
}

No point.

Dispose is for unmanaged resources.

Your class only contains managed resources.

teamList.Clear();
teamList = null;

Is completely unnecessary - the garbage collector handles that
just fine.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top