Skip to content

carlgv/DisposePractice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dispose Practice

This repository is the sample code developed to practice the using statement.

Implement of Dispose pattern on unmannaged resource

  • Generate new User class

    public class User: IDisposable

    and implement de interface on the class

    private bool disposedValue = false; // Para detectar llamadas redundantes
    
    protected virtual void Dispose(bool disposing)
    {
    	if (!disposedValue)
    	{
    		disposedValue = true;
    	}
    }
    public void Dispose()
    {
    	Dispose(true);
    	GC.SuppressFinalize(this);
    }
  • In Main, we can create a new User with using statement

    using (User u = new User())
    {
        // Some code	
    }

Invoke Dispose() on managed resources

  • Create a new instance of a managed resource
    StreamWriter swriter = new StreamWriter(path + @"\test.txt", true);
    and to release this resource:
    swriter.Dispose();

About

Practice with IDispose and using statement

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages