- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Default constructor in C#
A Constructor in C# is invoked automatically when an object gets created. The constructor has the same name as that of the class, for example −
public class Department { public Department () { Console.WriteLine("Default Constructor! "); } }
The following is the code that shows the usage of default constructor in C#. The constructor invokes immediately when the object gets created.
Department dept1 = new Department ();
A default constructor is a constructor that has no argument, for example −
Department () { }
Let us see the complete example to learn how to work with default contructors.
Example
using System; public class Department { public Department () { Console.WriteLine("Constructor Invoked"); } public static void Main(string[] args) { // object Department dept1 = new Department (); } }
Output
Constructor Invoked
Advertisements