Constructor and destructor are two seemingly simple types of functions in a class, but some unexpected running errors will always occur during actual use. This document introduces the principles of constructor and destructor systematically.
And the
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
Using
System;
Using
System. Collections. Generic;
Using
System. text; Namespace Staticdemo{ Public Class A{ Public Static
For some classes, it is best to have an initialization process when used for the first time in a program; when the program is no longer needed, it is best to do some finishing work, which is a very good class design habit.
Elicit questions
If this
The most basic function of a constructor is to assign an initial value to all the fields and properties in a new instance of the type. So, according to its function, he doesn't need (and doesn't make sense) the return value. His function name must
first, the definitionA static constructor is a new feature of C #, which seems to be seldom used. But we need to use it when we want to initialize some static variables. This constructor belongs to the class, not to the instance where it belongs,
(1) For initialization of static fields, read-only fields, and so on.
(2) Add the static keyword and cannot add an access modifier because the static constructor is private.
(3) The static constructor of a class executes at most once in a given
Using system;
Namespace Zoo
{
// ************ Define the base class animal
Public class animal
{
Static string baseclassname;
// Public String eatkind;
Protected string _ skincolor;
Static animal ()
{
Baseclassname = "animal
function | It is unavoidable to use static members when writing programs statically, because the access of static members is detached from the type object, so it is unreasonable to use non-static constructors, private methods, or some other methods.
I. Principle:Constructor is generally used to initialize the same name as the class of the object;The Destructor destroys the resources occupied by the object. "When an object is created, the constructor is automatically executed. When an object
Let's not talk nonsense. Let's look at a piece of code first:
View plaincopy to clipboardprint?
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace BeforeIniField
{
Class Program
{
Class
Li Jianzhong (Nanjing University of Posts and Telecommunications (Cornyfield@263.net)
Constructor
The constructor initializes the member variables (fields) in the class. The C # class has two constructor types: instance constructor and static
I took a test a while ago, and one of the multiple choice questions impressed me, as shown in the following figure:
The sequence in which an X-type object is instantiated:
A. Call the X-Type constructor, call the constructor of the X-type base class,
I was impressed by a multiple-choice question, as shown in the following figure:
The sequence in which an X-type object is instantiated:
A. Call the X-Type constructor, call the constructor of the X-type base class, and call the constructor of the X-
This section describes the instance constructor, private constructor, and static constructor. The role of constructor: 1. When a class is new to instantiate an object, you can call constructor to initialize the object. 2. A constructor without
This article mainly introduces the construction method of C # class. Have a certain reference value, follow the small series below to see it
First, the construction method
The constructor of a class is one of the member methods of a class, and its
/*Static fields, static properties, static methods, static constructs, static classes, Readonly,const, nested classes, partial class * Static can be understood as global, sharing the same block of memory * can be the default value of each type
A static constructor is a new feature of C #, which seems to be seldom used. But we need to use it when we want to initialize some static variables. This constructor belongs to the class, not to the instance where it belongs, meaning that the
Private constructorsA private constructor is a special instance constructor. It is typically used in classes that contain only static members. If a class has one or more private constructors and no public constructors, other classes (other than
constructor functionConstructors are divided into: instance constructors, static constructors, private constructors.Instance Constructors1, the name of the constructor is the same as the class name.2. When you use the new expression to create an
1. Static and non-static classes●. Static classes can only contain static members and static methods; otherwise, a compilation error is thrown;Non-static classes can contain non-static members and non-static methods.Static member and static method;●
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.