[{"data":1,"prerenderedAt":284},["ShallowReactive",2],{"i-mdi:theme-light-dark":3,"i-mdi:menu":8,"i-mdi:linkedin":10,"i-mdi:github":12,"i-mdi:email-outline":14,"article-\u002Farticles\u002Fserialization-how-to-save-objects-to-file":16,"i-mdi:arrow-left":282},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"M7.5 2c-1.79 1.15-3 3.18-3 5.5s1.21 4.35 3.03 5.5C4.46 13 2 10.54 2 7.5A5.5 5.5 0 0 1 7.5 2m11.57 1.5l1.43 1.43L4.93 20.5L3.5 19.07zm-6.18 2.43L11.41 5L9.97 6l.42-1.7L9 3.24l1.75-.12l.58-1.65L12 3.1l1.73.03l-1.35 1.13zm-3.3 3.61l-1.16-.73l-1.12.78l.34-1.32l-1.09-.83l1.36-.09l.45-1.29l.51 1.27l1.36.03l-1.05.87zM19 13.5a5.5 5.5 0 0 1-5.5 5.5c-1.22 0-2.35-.4-3.26-1.07l7.69-7.69c.67.91 1.07 2.04 1.07 3.26m-4.4 6.58l2.77-1.15l-.24 3.35zm4.33-2.7l1.15-2.77l2.2 2.54zm1.15-4.96l-1.14-2.78l3.34.24zM9.63 18.93l2.77 1.15l-2.53 2.19z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":11},"\u003Cpath fill=\"currentColor\" d=\"M19 3a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2zm-.5 15.5v-5.3a3.26 3.26 0 0 0-3.26-3.26c-.85 0-1.84.52-2.32 1.3v-1.11h-2.79v8.37h2.79v-4.93c0-.77.62-1.4 1.39-1.4a1.4 1.4 0 0 1 1.4 1.4v4.93zM6.88 8.56a1.68 1.68 0 0 0 1.68-1.68c0-.93-.75-1.69-1.68-1.69a1.69 1.69 0 0 0-1.69 1.69c0 .93.76 1.68 1.69 1.68m1.39 9.94v-8.37H5.5v8.37z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":13},"\u003Cpath fill=\"currentColor\" d=\"M12 2A10 10 0 0 0 2 12c0 4.42 2.87 8.17 6.84 9.5c.5.08.66-.23.66-.5v-1.69c-2.77.6-3.36-1.34-3.36-1.34c-.46-1.16-1.11-1.47-1.11-1.47c-.91-.62.07-.6.07-.6c1 .07 1.53 1.03 1.53 1.03c.87 1.52 2.34 1.07 2.91.83c.09-.65.35-1.09.63-1.34c-2.22-.25-4.55-1.11-4.55-4.92c0-1.11.38-2 1.03-2.71c-.1-.25-.45-1.29.1-2.64c0 0 .84-.27 2.75 1.02c.79-.22 1.65-.33 2.5-.33s1.71.11 2.5.33c1.91-1.29 2.75-1.02 2.75-1.02c.55 1.35.2 2.39.1 2.64c.65.71 1.03 1.6 1.03 2.71c0 3.82-2.34 4.66-4.57 4.91c.36.31.69.92.69 1.85V21c0 .27.16.59.67.5C19.14 20.16 22 16.42 22 12A10 10 0 0 0 12 2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"M22 6c0-1.1-.9-2-2-2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2zm-2 0l-8 5l-8-5zm0 12H4V8l8 5l8-5z\"\u002F>",{"id":17,"title":18,"body":19,"category":270,"description":32,"extension":271,"meta":272,"navigation":273,"path":274,"published":275,"seo":276,"stem":277,"tags":278,"__hash__":281},"articles\u002Farticles\u002Fserialization-how-to-save-objects-to-file.md","Serialization - how to save objects to file",{"type":20,"value":21,"toc":266},"minimark",[22,26,30,33,46,49,57,59,62,65,68,71,76,79,139,179,240,247,250,253,256,263],[23,24,25],"p",{},"In Object Oriented programming languages we are able to create new classes and create instances (objects) of these classes. These objects exists only if we construct them using constructor:",[27,28],"script",{"src":29},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F9d539c603d169911d5f4f8485481e1d1.js",[23,31,32],{},"But this option is good only if we want to create brand new object. What if we want to save object data to file and recover it later? We can either use StreamReader or serialization!",[23,34,35,36,41,42,45],{},"According to Microsoft C# documentation: \"Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.\"\nIt means that every object we create can be stored as a file and reconstructed whenever we want.\nHow to save object to file?\nThis process is easier than you expect. Let's begin with creating small class (Code available on my ",[37,38,40],"a",{"href":39},"https:\u002F\u002Fgithub.com\u002Fdawidstefaniak\u002FSerializationExample","GitHub","):\u000b",[27,43],{"src":44},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F839e7744f711dba2086b8e6b90162d2f.js","\nAnd create create object in main function:",[27,47],{"src":48},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F9a2ae533bed1271e158d24587b86bc99.js",[23,50,51,52,56],{},"To save object to file, we have to:\nMark class as Serializable using ",[53,54,55],"span",{},"Seriablizable"," (it allows as to store all object of class Dog):",[27,58],{"src":44},[23,60,61],{},"Add libraries to solution:",[27,63],{"src":64},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F9951b5238e665ef05cd1445dde77d10d.js",[23,66,67],{},"Add FileStream and BinaryFormatter objects:",[27,69],{"src":70},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F73fd3ce015af0d7599f4669eb5c0fc98.js",[72,73,75],"h4",{"id":74},"why-do-we-have-to-use-filestream-instead-of-streamreader-or-streamwriter-classes","Why do we have to use FileStream instead of StreamReader or StreamWriter classes?",[27,77],{"src":78},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F6f82acb2e10f0c28d49a478d77cb2114.js",[80,81,82,83],"table",{},"\n    ",[84,85,82,86,82,99,82,107,82,115,82,123,82,131,82],"tbody",{},[87,88,89,90,89,97,82],"tr",{},"\n        ",[91,92,93],"td",{},[94,95,96],"b",{},"FileMode.",[91,98],{},[87,100,89,101,89,104,82],{},[91,102,103],{},"Append",[91,105,106],{},"\n        Opens a file if it already exists and moves to the end ready to add\n        new data (if it doesn’t exist it is created)\n        ",[87,108,89,109,89,112,82],{},[91,110,111],{},"Create",[91,113,114],{},"Creates a new file or overwrites an existing one",[87,116,89,117,89,120,82],{},[91,118,119],{},"CreateNew",[91,121,122],{},"\n        Creates a new file or throws an IOException if the file already\n        exists\n        ",[87,124,89,125,89,128,82],{},[91,126,127],{},"OpenOrCreate",[91,129,130],{},"\n        Opens an existing file or creates it if it doesn’t already exist\n        ",[87,132,89,133,89,136,82],{},[91,134,135],{},"Truncate",[91,137,138],{},"\n        Opens an existing file and once opened truncates it so its size is\n        zero bytes\n        ",[80,140,82,141],{},[84,142,82,143,82,155,82,163,82,171,82],{},[87,144,89,145,89,153,82],{},[91,146,147,150],{},[94,148,149],{},"FileAccess",[94,151,152],{},".",[91,154],{},[87,156,89,157,89,160,82],{},[91,158,159],{},"Read",[91,161,162],{},"Data can be read from the file",[87,164,89,165,89,168,82],{},[91,166,167],{},"Write",[91,169,170],{},"Data can be written to the file",[87,172,89,173,89,176,82],{},[91,174,175],{},"ReadWrite",[91,177,178],{},"Data can be read from and written to the file",[80,180,82,181],{},[84,182,82,183,82,192,82,200,82,208,82,216,82,224,82,232,82],{},[87,184,89,185,89,190,82],{},[91,186,187],{},[94,188,189],{},"Properties",[91,191],{},[87,193,89,194,89,197,82],{},[91,195,196],{},"CanRead",[91,198,199],{},"\n        Gets a value indicating whether the FileStream supports reading\n        ",[87,201,89,202,89,205,82],{},[91,203,204],{},"CanWrite",[91,206,207],{},"Gets a value indicating whether a FileStream supports writing",[87,209,89,210,89,213,82],{},[91,211,212],{},"CanSeek",[91,214,215],{},"\n        Gets a value indicating whether the FileStream supports seeking\n        (moving to a specified position)\n        ",[87,217,89,218,89,221,82],{},[91,219,220],{},"Length",[91,222,223],{},"Gets the length of the FileStream in bytes",[87,225,89,226,89,229,82],{},[91,227,228],{},"Name",[91,230,231],{},"Gets the name of the FileStream",[87,233,89,234,89,237,82],{},[91,235,236],{},"Position",[91,238,239],{},"Gets or sets the current position of the FileStream",[23,241,242,246],{},[243,244,245],"strong",{},"BinaryFormatter"," on the other hand is one of the classes which give us ability to change our object to actual saveable data. The object of this class is taking the FileStream object and object which has to be serialized as its parameters.",[23,248,249],{},"Let's see how the code looks like after adding these objects:",[27,251],{"src":252},"https:\u002F\u002Fgist.github.com\u002Fdawidstefaniak\u002F27391acc97663a456ded3c99d52fcca3.js",[23,254,255],{},"As you probably noticed, you can use the same using(){} method as you can use with StreamReader and StreamWriter classes. If you don't use them, remember to always use .Close() method to close Stream connection. Please mind, that we can also use serialization with arrays.",[23,257,258,259,152],{},"These are just few of the results which we can receive using serialization. For more please visit ",[37,260,262],{"href":261},"https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fdotnet\u002Fcsharp\u002Fprogramming-guide\u002Fconcepts\u002Fserialization\u002F","Microsoft Webiste",[23,264,265],{},"That's all for now. If you have any questions don't hesitate to contact me!",{"title":267,"searchDepth":268,"depth":268,"links":269},"",2,[],"C#","md",{},true,"\u002Farticles\u002Fserialization-how-to-save-objects-to-file",null,{"title":18,"description":32},"articles\u002Fserialization-how-to-save-objects-to-file",[270,279,280],"CSharp","Serialization","VaHE5bvdxBDTMziS8McQMpPDajj26coYhXLcCjKuKyY",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":283},"\u003Cpath fill=\"currentColor\" d=\"M20 11v2H8l5.5 5.5l-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5L8 11z\"\u002F>",1779997852499]