ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Order of calling constructors(in C#)

.Net C# about 8 years ago || 4/29/2018 || 3.1 K View

  • Hide

Explanation:

Constructor works differently in Inheritance little bit confusing for new Programmers. There are two notions in execution of constructor.
1. Calling 
2. Execution
When you create an object of your derived class Named Derived the constructor first goes to Derived() then it goes to Parent() because of its calling. Constructor calling is made from Bottom to Top.
But then you find it Executes Parent() first then Derived, that is because of its Execution. Constructors Executed from Top to Bottom. That's why it print Parent first then Base while base constructor called first.

using System;

namespace InheritanceDemo {
 class Parent {
  public Parent() {
   Console.WriteLine("Hello From Parent");
  }
 }
 class Derived: Parent {
  public Derived() {
   Console.WriteLine("Hello from Derived");
  }
 }
 public class OrderOfExecution {
  public static void Main(string[] args) {
   Derived obj = new Derived();
  }
 }
}

Output:

Hello From Parent
Hello from Derived

 

  • 5
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Prakash Pokhrel

Prakash Pokhrel

https://np.linkedin.com/in/prakash-pokhrel-42a699a2

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey