Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lesson6

150 views

Published on

Learn C#

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Lesson6

  1. 1. Lesson 6 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-6
  2. 2. Agenda • Singleton pattern • Serialization
  3. 3. Singleton pattern class Singleton { private static Singleton _instance; public static Singleton Instance { get { if (_instance == null) { _instance = new Singleton(); } return _instance; } } //private constructor is neccessary in order to //prevent object cretion outside of a class private Singleton() { //ToDo: initialize class members } } Classic Singleton implementation class Singleton { public static Singleton Instance { get; } = new Singleton(); private Singleton() { //ToDo: initialize class members } } Singleton implementation with C# 6.0
  4. 4. Singleton Implementation • DEMO: see lesson 6.
  5. 5. Binary serialization • Serializatinon • Deserialization FileStream fs = new FileStream(_settingsFilePath, FileMode.OpenOrCreate);//Opening file for creation BinaryFormatter formatter = new BinaryFormatter();//BinaryFormatter instance is neccessary to convert object to bytes formatter.Serialize(fs, Instance);//converting bytes to file.... fs.Close();//closing file stream to avoid issues with this file further. FileStream fs = new FileStream(_settingsFilePath, FileMode.Open);//Opening file for creation BinaryFormatter formatter = new BinaryFormatter();//BinaryFormatter instance is neccessary to convert bytes to object var storedSettings = (Settings) formatter.Deserialize(fs);//converting file to object fs.Close();//closing file stream to avoid issues with this file further.
  6. 6. Thank you! Questions?

×