c# - Binary serialization, IFormatter: use a new one each time or store one in a field? -


using binary formatting 1st time in .net c#

code msdn this:

 iformatter formatter = new binaryformatter();  stream stream = new filestream("myfile.lvl", filemode.create, fileaccess.write,fileshare.none);  formatter.serialize(stream, globals.currentlevel);  stream.close(); 

just wondering should store iformatter in field in class , use on , on again or should above , instantiate new 1 every time save/load something?

i noticed not idisposable.

there's little overhead in re-creating binaryformatter, of properties sets in constructor enums, see here (thanks reflector):

public binaryformatter() {     this.m_typeformat = formattertypestyle.typesalways;     this.m_securitylevel = typefilterlevel.full;     this.m_surrogates = null;     this.m_context = new streamingcontext(streamingcontextstates.all); } 

if going re-use though, you'd need synchronize access serialize , deserialize methods keep them thread-safe.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -