Beispiel #1
0
 void Start()
 {
     d = new DisposableStruct(0);
     Debug.Log(d.id + " before");
     RefReturn(ref d);
     Debug.Log(d.id + " after");
 }
Beispiel #2
0
        public void Test()
        {
            IAlreadyImplemented already = new AlreadyImplemented();

            if (already.IsAdded())
            {
                Console.WriteLine("Already implemented!!!");
            }
            //Using of pattern
            PointV8 pointV8 = new PointV8()
            {
                X = 1, Y = 2, Rainbow = Rainbow.Blue
            };

            Console.WriteLine($"Multiply: {Multiply(pointV8)}");
            Console.WriteLine($"Quadrant: {GetQuadrant(pointV8)}");
            Console.WriteLine($"Rainbow: {FromRainbow(pointV8.Rainbow)}");
            Console.WriteLine($"Rock vs Paper wins: {RockPaperScissors(Rps.Rock, Rps.Paper)}");
            Console.WriteLine($"Ï want to take a taxi and toll is: {CalculateToll(new Taxi())}");
            //It's disposed when it's out of scope.
            using StreamWriter streamWriter = new StreamWriter(new MemoryStream());
            streamWriter.Write("Close this stream writer at the end of this method");
            //If I add to a ref struct the Dispose() method, i can use using statement to dispose it at the end of scope
            using DisposableStruct disposableStruct = new DisposableStruct();
            bool isWhite = false;

            Console.WriteLine("It's not white: " + IsNotWhite(isWhite));
Beispiel #3
0
    /// <summary>
    /// struct类型的IDisposable,如果给另外一个赋值了,在另一个对象无用之后,需要手动Dispose
    /// </summary>
    private void assignment()
    {
        DisposableStruct temp = d;

        temp.id = id++;
        temp.Dispose();         // **
    }
Beispiel #4
0
        // ERROR: ref structs only live in the stack
        //private DisposableStruct inClass;

        public void TryDisposing()
        {
            // we can create and dispose structs
            using (var instance = new DisposableStruct())
            {
                DoSomethingSmart(instance);
            }
        }
Beispiel #5
0
    private void RefReturn(ref DisposableStruct info)
    {
        DisposableStruct temp = info;

        temp.id = id++;
        info.Dispose();
        info = new DisposableStruct(id++);
        temp.Dispose();
    }
Beispiel #6
0
        public static void IDisposableStruct()
        {
            var s = new DisposableStruct();

            Disposable <DisposableStruct> .Dispose(s);

            Equal(1, s.Disposed);
            Disposable <DisposableStruct> .Dispose(s);

            Equal(2, s.Disposed);
        }
Beispiel #7
0
    static void Main()
    {
        string text = "abc";
        foreach (var foo in text) { }

        // no boxing
        using (var a = new DisposableStruct()) { }
        using (DisposableStruct.Instance) { }

        // yeah, no boxing too
        using (var b = new BadDisposableStruct()) { }
        using (BadDisposableStruct.Instance) { }
    }
Beispiel #8
0
        public void DisposeStruct()
        {
            var count = 0;
            var ds    = new DisposableStruct(() => count++);

            // disposing should invoke the action
            DisposableUtility.DisposeObject(ref ds);
            Assert.AreEqual(1, count);

            // struct should have been replaced with default value, preventing second increment
            DisposableUtility.DisposeObject(ref ds);
            Assert.AreEqual(1, count);
        }
Beispiel #9
0
    static void Main()
    {
        string text = "abc";

        foreach (var foo in text)
        {
        }

        // no boxing
        using (var a = new DisposableStruct()) { }
        using (DisposableStruct.Instance) { }

        // yeah, no boxing too
        using (var b = new BadDisposableStruct()) { }
        using (BadDisposableStruct.Instance) { }
    }
Beispiel #10
0
        public void Print()
        {
            ConsoleColor currentBackground = Console.BackgroundColor;
            ConsoleColor currentForeground = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine($"pre using declared");
            using var disposableClass  = new DisposableClass();
            using var disposableStruct = new DisposableStruct();
            // different from using (var d = new DisposableClass())

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"post using declared ");

            Console.BackgroundColor = currentBackground;
            Console.ForegroundColor = currentForeground;

            Console.WriteLine($"After this line the objects dispose");
        }
 public static void Run()
 {
     using var disposableStruct = new DisposableStruct();
 }
Beispiel #12
0
 private void DoSomethingSmart(DisposableStruct instance)
 {
     var size = instance.FileSize;
 }