Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy initialization
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C#=== In .NET Framework 4.0 Microsoft has included a <code>Lazy</code> class that can be used to do lazy loading. Below is some dummy code that does lazy loading of Class <code>Fruit</code> <syntaxhighlight lang="csharp"> var lazyFruit = new Lazy<Fruit>(); Fruit fruit = lazyFruit.Value; </syntaxhighlight> Here is a dummy example in [[C Sharp (programming language)|C#]]. The <code>Fruit</code> class itself doesn't do anything here, The [[class variable]] <code>_typesDictionary</code> is a Dictionary/Map used to store <code>Fruit</code> instances by <code>typeName</code>. <syntaxhighlight lang="csharp"> using System; using System.Collections; using System.Collections.Generic; public class Fruit { private string _typeName; private static IDictionary<string, Fruit> _typesDictionary = new Dictionary<string, Fruit>(); private Fruit(string typeName) { this._typeName = typeName; } public static Fruit GetFruitByTypeName(string type) { Fruit fruit; if (!_typesDictionary.TryGetValue(type, out fruit)) { // Lazy initialization fruit = new Fruit(type); _typesDictionary.Add(type, fruit); } return fruit; } public static void ShowAll() { if (_typesDictionary.Count > 0) { Console.WriteLine("Number of instances made = {0}", _typesDictionary.Count); foreach (KeyValuePair<string, Fruit> kvp in _typesDictionary) { Console.WriteLine(kvp.Key); } Console.WriteLine(); } } public Fruit() { // required so the sample compiles } } class Program { static void Main(string[] args) { Fruit.GetFruitByTypeName("Banana"); Fruit.ShowAll(); Fruit.GetFruitByTypeName("Apple"); Fruit.ShowAll(); // returns pre-existing instance from first // time Fruit with "Banana" was created Fruit.GetFruitByTypeName("Banana"); Fruit.ShowAll(); Console.ReadLine(); } } </syntaxhighlight> A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an [[Enumerated type|enumeration]] for the type <syntaxhighlight lang="csharp"> namespace DesignPatterns.LazyInitialization; public class LazyFactoryObject { // internal collection of items // IDictionary makes sure they are unique private IDictionary<LazyObjectSize, LazyObject> _LazyObjectList = new Dictionary<LazyObjectSize, LazyObject>(); // enum for passing name of size required // avoids passing strings and is part of LazyObject ahead public enum LazyObjectSize { None, Small, Big, Bigger, Huge } // standard type of object that will be constructed public struct LazyObject { public LazyObjectSize Size; public IList<int> Result; } // takes size and create 'expensive' list private IList<int> Result(LazyObjectSize size) { IList<int> result = null; switch (size) { case LazyObjectSize.Small: result = CreateSomeExpensiveList(1, 100); break; case LazyObjectSize.Big: result = CreateSomeExpensiveList(1, 1000); break; case LazyObjectSize.Bigger: result = CreateSomeExpensiveList(1, 10000); break; case LazyObjectSize.Huge: result = CreateSomeExpensiveList(1, 100000); break; case LazyObjectSize.None: result = null; break; default: result = null; break; } return result; } // not an expensive item to create, but you get the point // delays creation of some expensive object until needed private IList<int> CreateSomeExpensiveList(int start, int end) { IList<int> result = new List<int>(); for (int counter = 0; counter < (end - start); counter++) { result.Add(start + counter); } return result; } public LazyFactoryObject() { // empty constructor } public LazyObject GetLazyFactoryObject(LazyObjectSize size) { // yes, i know it is illiterate and inaccurate LazyObject noGoodSomeOne; // retrieves LazyObjectSize from list via out, else creates one and adds it to list if (!_LazyObjectList.TryGetValue(size, out noGoodSomeOne)) { noGoodSomeOne = new LazyObject(); noGoodSomeOne.Size = size; noGoodSomeOne.Result = this.Result(size); _LazyObjectList.Add(size, noGoodSomeOne); } return noGoodSomeOne; } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)