C # Inheritance: how to call the constructor of a base class when I call the constructor of a derived class

I am trying to figure out how to call the constructor of the base class when calling the constructor of the derived class.

I have a class called "AdditionalAttachment" that is inherited from System.Net.Mail.Attachment. I added 2 more properties to my new class so that I have all the properties of the existing Attachment class with my new properties

public class AdditionalAttachment: Attachment { [DataMember] public string AttachmentURL { set; get; } [DataMember] public string DisplayName { set; get; } } 

I used to create a type constructor

// objMs - MemoryStream object

 Attachment objAttachment = new Attachment(objMs, "somename.pdf") 

I am wondering how I can create the same constructor for my class that will do the same as the above base class constructor

+6
inheritance constructor c #
source share
4 answers

This will pass your parameters to the base class constructor:

 public AdditionalAttachment(MemoryStream objMs, string displayName) : base(objMs, displayName) { // and you can do anything you want additionally // here (the base class constructor will have // already done its work by the time you get here) } 
+13
source share

You can write a constructor that calls the constructor of the class base:

 public AdditionalAttachment(MemoryStream objMs, string filename) : base(objMs, filename) { } 
+7
source share

Use this function:

 public AdditionalAttachment(MemoryStream ms, string name, etc...) : base(ms, name) { } 
+7
source share
 public class AdditionalAttachment: Attachment { public AdditionalAttachment(param1, param2) : base(param1, param2){} [DataMember] public string AttachmentURL { set; get; } [DataMember] public string DisplayName { set; get; } } 
+3
source share

All Articles