Поделиться через


Constructors (C# Programming Guide)

Constructors are class methods that are executed when an object of a class or struct is created. They have the same name as the class or struct, and usually initialize the data members of the new object.

A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.

If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values as listed in Default Values Table (C# Reference). Static classes and structs can also have constructors.

In This Section

Using Constructors (C# Programming Guide)

Instance Constructors (C# Programming Guide)

Private Constructors (C# Programming Guide)

Static Constructors (C# Programming Guide)

How to: Write a Copy Constructor (C# Programming Guide)

See Also

Reference

Classes and Structs (C# Programming Guide)

Destructors (C# Programming Guide)

static (C# Reference)

Concepts

C# Programming Guide

Constructor Design

Other Resources

Why Do Initializers Run In The Opposite Order As Constructors? Part One