Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
BG19bot (talk | contribs)
m WP:CHECKWIKI error fix for #61. Punctuation goes before References. Do general fixes if a problem exists. - using AWB (10511)
Merged .NET specific definition for "cctor" into C# section; added abbreviation and notice to copy ctor.
Line 3:
 
In [[class-based programming|class-based]] [[object-oriented programming]], a '''constructor''' (abbreviation: '''ctor''') in a [[Class (computer programming)|class]] is a special type of [[subroutine]] called to [[object creation|create an object]]. It prepares the new object for use, often accepting [[argument]]s that the constructor uses to set required [[member variable]]s.
 
'''Instance constructors''', sometimes referred to as '''.ctor''', are used to create and initialize any instance member variables when the new expression is used to create an object of a class. To initialize a static class, or static variables in a non-static class, a '''static constructor''' must be defined. Static constructors are sometimes referred to as '''.cctor'''.<ref>{{cite web|url=http://msdn.microsoft.com/en-us/library/k6sa6h87.aspx|title=Instance Constructors (C# Programming Guide) |publisher=Microsoft Developers Networks |accessdate=2014-04-05}}</ref><ref>{{cite book|url=http://books.google.com/books?id=oAcCRKd6EZgC&pg=PA222&lpg=PA222&dq=ctor+cctor&source=bl&ots=KAYfbDu_ay&sig=gLj6X9SpWXz86GHMvljkfb-D8Oc&hl=en&sa=X&ei=fUFAU--WJOewsASQ54GQAw&ved=0CGoQ6AEwCQ#v=onepage&q=ctor%20cctor&f=false |title=Expert .NET 2.0 IL Assembler |publisher=APress |date=2006-01-01|accessdate=2014-04-05}}</ref>
 
A constructor resembles an [[method (computer science)|instance method]], but it differs from a method in that it has no explicit [[return type]], it is not implicitly [[inheritance (object-oriented programming)|inherited]] and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring [[class (computer science)|class]]. They have the task of [[initialization (computing)|initializing]] the object's [[data member]]s and of establishing the [[invariant (computer science)|invariant]] of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting [[object (computer science)|object]] in a ''valid'' state. [[Immutable object]]s must be initialized in a constructor.
Line 55 ⟶ 53:
It is used to create a copy of an existing object of the same
class. Even though both classes are the same, it counts as a conversion
constructor.
 
While copy constructors are usually abbreviated '''copy ctor''' or '''cctor''', they have nothing to do with ''class constructors'' used in .NET using the same abbreviation.
 
=== Conversion constructors ===
Line 184:
==== C# static constructor ====
 
In [[C Sharp (programming language)|C#]], a ''static constructor'' is a static data initializer. Static constructors are also called ''class constructors''. Since the actual method generated has the name ''.cctor'' they are often also called "cctors".<ref>{{cite web|url=http://ericlippert.com/2013/02/06/static-constructors-part-one/ |title=Fabulous Adventures in Coding |publisher=Eric Lippert |date=2013-02-06|accessdate=2014-04-05}}</ref><ref>{{cite book|url=http://books.google.com/books?id=oAcCRKd6EZgC&pg=PA222&lpg=PA222&dq=ctor+cctor&source=bl&ots=KAYfbDu_ay&sig=gLj6X9SpWXz86GHMvljkfb-D8Oc&hl=en&sa=X&ei=fUFAU--WJOewsASQ54GQAw&ved=0CGoQ6AEwCQ#v=onepage&q=ctor%20cctor&f=false |title=Expert .NET 2.0 IL Assembler |publisher=APress |date=2006-01-01|accessdate=2014-04-05}}</ref>
 
Static constructors allow complex static variable initialization.<ref>[http://msdn.microsoft.com/en-us/library/k9x6w0hc%28VS.80%29.aspx Static Constructor in C# on MSDN]</ref>