GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • C# Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Constructor in C# - SyntaxDB - C# Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Write to Console
      • Commenting
      • User Input
      • Read from File
      • Write to File
      • Namespace
    • Variables
      • Primitive Data Types
      • Variable Declaration
      • Parse
      • Constants
      • Pointers and References
      • Arrays
      • Lists
      • Dictionaries
      • Type Conversion
      • Structures
    • Control Flow
      • If Statement
      • Switch Case
      • For Loop
      • For Each
      • While Loop
      • Do While Loop
      • Try Catch Block
    • Object Oriented Programming
      • Class Declaration and Structure
      • Access Modifiers
      • Class Variables
      • Standard Methods
      • Static Methods
      • Constructor
      • Instantiation
      • Interfaces
      • Abstract Methods and Classes
      • Inheritance
    • Strings
      • String Variables
      • String Length
      • String Compare
      • String Trim
      • String Split
      • String Representation
Categories
Language
C#
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 5

Categories

  • User Program Communication
    • Write to Console
    • Commenting
    • User Input
    • Read from File
    • Write to File
    • Namespace
  • Variables
    • Primitive Data Types
    • Variable Declaration
    • Parse
    • Constants
    • Pointers and References
    • Arrays
    • Lists
    • Dictionaries
    • Type Conversion
    • Structures
  • Control Flow
    • If Statement
    • Switch Case
    • For Loop
    • For Each
    • While Loop
    • Do While Loop
    • Try Catch Block
  • Object Oriented Programming
    • Class Declaration and Structure
    • Access Modifiers
    • Class Variables
    • Standard Methods
    • Static Methods
    • Constructor
    • Instantiation
    • Interfaces
    • Abstract Methods and Classes
    • Inheritance
  • Strings
    • String Variables
    • String Length
    • String Compare
    • String Trim
    • String Split
    • String Representation

Object Oriented Programming

Constructor in C#

A constructor is a special method that builds the object when a new object is created.


Syntax
modifier className (dataType parameter1, dataType parameterN) { 
   //initalizations of class fields in terms of defined parameters. 
} 

Notes

The name of the constructor must be the same as the name of the class.

Constructors are not considered members of a class and is called automatically when a new instance of an object is created.

A constructor is used to initialize fields defined by the class.


Example
public class Scores {
    string name;
    int score;
    ///constructor
    public Scores (string studentName, int examMark) { 
       name = studentName; 
       score = examMark; 
    }
}

Add to Slack

< Static Methods   |   Instantiation >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.