C # Learning Notes Collation _ In-depth analysis of constructors, destructors _c# tutorials

Source: Internet
Author: User
Tags datetime garbage collection modifiers

constructors, destructors

constructor function:

1. If no constructor is provided, the system automatically provides a default constructor that initializes all members as default values (reference type is null reference NULL, value type is 0,bool type is false);

2. If a constructor with a parameter is provided, the system does not provide a default constructor function;

3. Constructors can be overloaded: can provide a number of different versions of the constructor, based on the number of parameters, types to distinguish;

4. Private constructor: The object cannot be instantiated through the constructor, can be instantiated by calling a static function, and when used only as a container for some static members or properties, a private constructor can be defined to prevent instantiation;

General constructors are instance constructors that execute constructors whenever an instance is created;

Static constructors:

1. Only one can be defined, run up to one time, and only before any member of the class is invoked for the first time. NET runtime to call it,

2. It can not take any parameters, no access modifiers, can only access the static members of the class, can not access to the class instance members;

3. If the class has some static fields, the property needs to initialize from the external source before the class is first used, the static constructor is used;

4. Static constructors and parameterless instance constructors can be defined at the same time, and when to execute which constructors do not conflict;

You can call other constructors of your own in the constructor: this (), or the constructor of the parent class: base (), which can invoke other constructors like inherited syntax;

Read-only field readonly: similar to constant const, you cannot modify its value, but a read-only field is declared with ReadOnly, can be uninitialized, initialized in a constructor, and cannot be changed after its value;

Instantiation of anonymous Type: var a = new{f1= "1AD", "f2=", F3=5, f4=6};

Constructors: Instances for destructor classes

• Destructors cannot be defined in structs. Destructors can only be used on classes.

• A class can have only one destructor.

• cannot inherit or overload a destructor.

• Destructors cannot be invoked. They are called automatically. Controlled by the garbage collector, if the garbage collector considers an object to conform to the destructor, call the destructor (if any) and reclaim the memory used to store the object. Destructors are also called when the program exits

• Destructors have no modifiers and no parameters

You can force garbage collection by invoking Collect, but in most cases you should avoid doing so because this can cause performance problems

C # does not require too much memory management. This is because the. NET Framework garbage collector implicitly manages the memory allocation and release of objects. However, when an application encapsulates unmanaged resources such as Windows, files, and network connections, you should use destructors to release these resources. When an object conforms to the destructor, the garbage collector runs the Finalize method of the object.

If your application is using expensive external resources, we also recommend that you provide a way to explicitly release resources before the garbage collector frees objects. You can do this by implementing the Dispose method from the IDisposable interface, which performs the necessary cleanup for the object. This can greatly improve the performance of your application. Even with this explicit control of resources, destructors are a protective measure that can be used to clean up resources when a call to the Dispose method fails

Class car

{

  -car () 

  {

    //...

  }

}

This destructor implicitly calls the Finalize () method recursively for all instances in the inheritance chain

public class Bus {protected static readonly DateTime globalstarttime;
 
   protected int Routenumber {get; set;}
 Static bus ()//static constructor {globalstarttime = DateTime.Now;
   Console.WriteLine ("Static ctor sets global start time to {0}", globalstarttime.tolongtimestring ());
 (int routenum) {routenumber = Routenum;
   Console.WriteLine ("{0} is created.", Routenumber);
 
 public void Drive () {TimeSpan elapsedtime = datetime.now-globalstarttime; Console.WriteLine (' {0} is starting it route {1:n2} minutes after global start time {2}. ', this.
   Routenumber, Elapsedtime.totalmilliseconds, globalstarttime.toshorttimestring ());
 } class Testbus {static void Main () = Bus bus = new bus (71); Bus.
 Drive ();
 
 System.Threading.Thread.Sleep (25);
 Bus Bus2 = new bus (72); Bus2.
 
 Drive ();
 System.Console.WriteLine ("Press any key to exit.");
   System.Console.ReadKey (); }}/* output:staticctor sets global start time to 10:04:08 AM are created.
   The is starting it route 21.00 minutes after global start time 10:04 AM.
   The is created.   
The is starting its route 46.00 minutes over global start time 10:04 AM. */

The above is a small series for everyone to bring the C # Learning notes to organize in-depth analysis of the constructor, destructor, all the contents of the function, I hope to help you, a lot of support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.