void Start() { d = new DisposableStruct(0); Debug.Log(d.id + " before"); RefReturn(ref d); Debug.Log(d.id + " after"); }
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));
/// <summary> /// struct类型的IDisposable,如果给另外一个赋值了,在另一个对象无用之后,需要手动Dispose /// </summary> private void assignment() { DisposableStruct temp = d; temp.id = id++; temp.Dispose(); // ** }
// 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); } }
private void RefReturn(ref DisposableStruct info) { DisposableStruct temp = info; temp.id = id++; info.Dispose(); info = new DisposableStruct(id++); temp.Dispose(); }
public static void IDisposableStruct() { var s = new DisposableStruct(); Disposable <DisposableStruct> .Dispose(s); Equal(1, s.Disposed); Disposable <DisposableStruct> .Dispose(s); Equal(2, s.Disposed); }
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) { } }
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); }
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(); }
private void DoSomethingSmart(DisposableStruct instance) { var size = instance.FileSize; }