示例#1
0
        static void Main(string[] args)
        {
            var foo1 = new FooSealed();

            foo1.GetHashCode();
            // needs to be disposed. That's what the interface is telling you.
            //foo1.Dispose();


            var foo2 = new Foo2();

            try
            {
                // use foo
                foo2.GetHashCode();
            }
            finally
            {
                // failure to
                foo2.Dispose();
            }


            // a using statement is an easier way to define the above
            using (var foo3 = new Foo3())
            {
                foo3.GetHashCode();
            }


            // even easier way of using a using statement
            using var foo4 = new Foo4();
            foo4.GetHashCode();

            var a = "Do something else";

            foo4.GetHashCode(); // can carry on using 'using'


            // sometimes a dispose will call dispose on an underlying object as well.
            // no need to wrap stream in using here, stream writer will call dispose on the stream passed in
            // wrapping it could be misleading as it would indicate stream was still usable after the inner using
            FileStream stream = new FileStream(Guid.NewGuid().ToString() + ".txt", FileMode.CreateNew);

            // Create a StreamWriter from FileStream
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.Write("Hello StreamWriter");
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            object a = new { };

            GC.KeepAlive(a);                   // a fake reference to an object. Garbage collector won't delete as there is still a reference (immediately after this line the GC CAN remove it) up to this line, not forever
            GC.Collect();                      // Forces a garbage collection to happen now

            GC.SuppressFinalize(a);            // stops the finialze method from being called at any point, often used in dispose method to stop it occurring twice
            GC.ReRegisterForFinalize(a);       // undoes the above, when GC runs if no references, will collect

            GC.AddMemoryPressure(123);         // lets the GC know that a large amount of unmanaged memory has been allocated, this is so the finalize method will be called sooner
            GC.RemoveMemoryPressure(123);      // lets the GC know that a large amount of unmanged memory has been released

            var ptr = Marshal.AllocHGlobal(1); // allocate memory on unmanged heap

            Marshal.FreeHGlobal(ptr);          // free it


            var foo1 = new FooSealed();

            foo1.GetHashCode();
            // needs to be disposed. That's what the interface is telling you.
            //foo1.Dispose();


            var foo2 = new Foo2();

            try
            {
                // use foo
                foo2.GetHashCode();
            }
            finally
            {
                // failure to
                foo2.Dispose();
            }


            // a using statement is an easier way to define the above
            using (var foo3 = new Foo3())
            {
                foo3.GetHashCode();
            }


            // even easier way of using a using statement
            using var foo4 = new Foo4();
            foo4.GetHashCode();

            var a = "Do something else";

            foo4.GetHashCode(); // can carry on using 'using'


            // sometimes a dispose will call dispose on an underlying object as well.
            // no need to wrap stream in using here, stream writer will call dispose on the stream passed in
            // wrapping it could be misleading as it would indicate stream was still usable after the inner using
            FileStream stream = new FileStream(Guid.NewGuid().ToString() + ".txt", FileMode.CreateNew);

            // Create a StreamWriter from FileStream
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.Write("Hello StreamWriter");
            }
        }