zoukankan      html  css  js  c++  java
  • Using .NET 4's Lazy<T> 实现单实例

    Using .NET 4's Lazy<T> type
     
    Explanation of the following code:

    1. If you're using .NET 4 (or higher) then you can use the System.Lazy<T> type to make the laziness really simple.
    2. All you need to do is pass a delegate to the constructor that calls the Singleton constructor, which is done most easily with a lambda expression.
    3. It also allows you to check whether or not the instance has been created with the IsValueCreated property.

    public sealed class Singleton

    {

        private Singleton()

        {

        }

        private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());

        public static Singleton Instance

        {

            get

            {

                return lazy.Value;

            }

        }

    }
     

    Example:
     
    The final example is here:

    namespace Singleton

    {

        class Program

        {

            static void Main(string[] args)

            {

                Calculate.Instance.ValueOne = 10.5;

                Calculate.Instance.ValueTwo = 5.5;

                Console.WriteLine("Addition : " + Calculate.Instance.Addition());

                Console.WriteLine("Subtraction : " + Calculate.Instance.Subtraction());

                Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());

                Console.WriteLine("Division : " + Calculate.Instance.Division());

                Console.WriteLine(" ---------------------- ");

                Calculate.Instance.ValueTwo = 10.5;

                Console.WriteLine("Addition : " + Calculate.Instance.Addition());

                Console.WriteLine("Subtraction : " + Calculate.Instance.Subtraction());

                Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());

                Console.WriteLine("Division : " + Calculate.Instance.Division());

                Console.ReadLine();

            }

        }

        public sealed class Calculate

        {

            private Calculate()

            {

            }

            private static Calculate instance = null;

            public static Calculate Instance

            {

                get

                {

                    if (instance == null)

                    {

                        instance = new Calculate();

                    }

                    return instance;

                }

            }

            public double ValueOne { get; set; }

            public double ValueTwo { get; set; }

            public double Addition()

            {

                return ValueOne + ValueTwo;

            }

            public double Subtraction()

            {

                return ValueOne - ValueTwo;

            }

            public double Multiplication()

            {

                return ValueOne * ValueTwo;

            }

            public double Division()

            {

                return ValueOne / ValueTwo;

            }

        }

    }

  • 相关阅读:
    常见的概念
    cas底层
    判断页面是否读取了缓存
    window.location.hash(hash应用)---跳转到hash值制定的具体页面
    * 输入框被第三方输入法遮挡问题
    Mui去掉滚动条:
    实用网址
    完美解决safari、微信浏览器下拉回弹效果。
    移动端兼容性问题解决方案
    监听ios自带返回功能
  • 原文地址:https://www.cnblogs.com/zeroone/p/3618634.html
Copyright © 2011-2022 走看看