
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is the default constructor in C#?
A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A default constructor does not have any parameter.
The following is an example showing how to work with default constructor in C# −
Example
using System; namespace LineApplication { class Line { private double length; // Length of a line public Line(double len) { //Parameterized constructor Console.WriteLine("Object is being created, length = {0}", len); length = len; } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(10.0); Console.WriteLine("Length of line : {0}", line.getLength()); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } }
Output
Object is being created, length = 10 Length of line : 10 Length of line : 6
- Related Articles
- What is a default constructor in JavaScript?
- What is default constructor in C# programs?
- What is the purpose of a default constructor in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- Default constructor in C#
- Default constructor in Java
- C++ default constructor
- Java default constructor
- What do you mean by default constructor in Java?
- How to create a default constructor in Java?
- Can we initialize static variables in a default constructor in Java?
- Do static variables get initialized in a default constructor in java?
- What is the use of constructor in Java?
- What is the Default Gateway?
- What is converting constructor in C++ ?

Advertisements