Destructor (C#)

I have made a FileLogger to check on some values because it helps debugging. But i want to close it properly when he FileLogger is destroyed and Unity3D does not seem to accept Destructors, why?

public class FileLogger<T>  {

	static  FileLogger<T> instance;
	FileStream fs_;
	StreamWriter sw_;
	static int logcounter_;
	public const  string Filename="Log.txt";
     FileLogger ()
    {
	    fs_= new FileStream (Filename,FileMode.Create);
		sw_= new StreamWriter (fs_);
		logcounter_=0;
    }	
	public static FileLogger<T> GetInstance()
	{
		if (instance==null)
			instance=new FileLogger<T> ();
		return instance;
	}
	
	public void LogToFile (T val)
	{
	sw_.WriteLine ("Log "+logcounter_+" : "+  val.ToString());
		++ logcounter_;
	}
	public void LogToFile (string s,T val)
	{
	sw_.WriteLine ("Log "+logcounter_+" : "+s+ val.ToString());
		++ logcounter_;
	}
	public void CloseFile()
	{
		fs_.Close();
		sw_.Close();
	}
	
	/*public ~FileLogger () 
	{
		CloseFile();
	}*/
	
}

P.S. Maybe the script names should accept <> because i had to make Bogus empty class to name the script.

I’m still new to c# - I learned in c++. But here’s my 2 cents: my understanding is that for disposal of unmanaged resources like files, the IDisposable Interface should be used. A search on C# IDisposable Interface will give you some articles.

put a second class in without the and the editor will no longer bring up errors / warnings.

Thx, me too i’m a big C++ fan! I miss the generic options that the language offers and much more. The .Net framework is nice for everyday apps but for video games the garbage collector sucks ***…

By the way Idisposable can help me on that subject but i just wanted to know why destructors were’nt tolerated. By the parsing error number, it comes from the Mono implementation of C#.

I believe you shouldn’t be assigning an access modifier to the destructor, so try removeing the “public” keyword from the destructor declaration. You never call the destructor in your own code in C#, the garbage collector is the only one that can call it.

Something else worth noting is that the compiler should automatically turn your destructor into an “Object.Finalize()” function, you can read more about that here.

I’ve come from a C++ background, and it did take me a while to get used to not deleting and clearing up my own memory, but when you see how much it frees you up to focus on writing your games, rather than writing code to clean up after yourself, I think you will find it is a much better way to work. At least for prototyping ;).

Thx it worked.I thought i tried that, but i guess i did not!