C# FAQ: How can one constructor call another

Jump to: navigation, search

How can one constructor call another?

To call one C# constructor from another, before the body of the constructor, use either:
: base (parameters)

to call a constructor in the base class; or:

: this (parameters)

to call a constructor in this class.

The following examples illustrate how to call one constructor from another.

public class Supervisor : Employee
{
   public Supervisor (int vacationDays) : base (vacationDays)
   {
      //Add more statements here.
   }
}

In the prededing example, the base class constructor is called before the constructor block is executed. The base keyword is usable with or without parameters. Any constructor parameters can be used either as part of an expression or as parameters to base.

public class Employee
{
   public Employee(int weeklyOvertime)
   {
      overtime = weeklyOvertime;
   } 
 
   public Employee (int overtimeRate, int numberHours)
      : this (overtimeRate * numberHours)
   {
      //Add more statements here.
   }
}


More CSharp Language FAQs

Go to Visual CSharp FAQ (main page).

Consider these related Frequently Asked Questions—FAQs:

  1. Are C# constructors inherited?
  2. Are C# parameters passed by reference or by value ?
  3. Can a property have different get and set access?
  4. Can const and static be used together?
  5. Can I directly call a native function exported from a DLL?
  6. Do unused references to external assemblies lead to code bloat?
  7. Does C# have its own class library?
  8. Does C# have macros or a preprocessor?
  9. Does C# support custom exceptions?
  10. Does C# support jagged arrays?
  11. Does C# support variable method arguments (vararg)?
  12. How call overloaded operators from non-supporting languages?
  13. How can I force garbage collection?
  14. How can I output simple debugging messages?
  15. How can one constructor call another?
  16. How check the object type at runtime?
  17. How can type name clashes be resolved?
  18. How do destructors work in C#?
  19. How do I declare an out variable?
  20. How do I get DllImport to work?
  21. How do I make a C# DLL?
  22. How do I use trace and assert?
  23. How get the type name at runtime?
  24. How keep a local variable in scope across a try-catch block?
  25. How make sure C# classes will interoperate with other .NET languages?
  26. How perform a case insensitive string comparison?
  27. How process command line arguments?
  28. How specify a C# literal type?
  29. How subscribe to events exposed by remoted objects?
  30. How to obtain type information on the fly using typeof?
  31. How use an alias for a class or namespace?
  32. How would I create a Delegate or MulticastDelegate?
  33. Is there C# support for C-type macros?
  34. Is there C# support for templates?
  35. What are the C# character escape sequences (\)?
  36. What does at sign (@) identifier mean?
  37. What does "Object reference not set to an instance of an object" mean?
  38. What is a namespace?
  39. What is a type alias?
  40. What is C#?
  41. What is a C# response file?
  42. What is a verbatim string literal (@)?
  43. What is the C# Language Specification?
  44. What is the #define preprocessor directive?
  45. What is the difference between const and static readonly?
  46. What is the difference between delegate and event?
  47. What is the difference between GetType() and typeof?
  48. What is the difference between new and override?
  49. What is the difference between the out and ref parameter modifiers?
  50. What is the difference between string and System.String?
  51. What is the difference between using a cast and the as operator?
  52. What is the syntax for calling an overloaded constructor from within a constructor?
  53. What is the use of the C# compiler's /target: command line option?
  54. What is the Windows Class Viewer?
  55. What software is needed to develop C# applications?
  56. When should an exception be thrown?
  57. When should Equals and == be used?
  58. Will Console.WriteLine() stop printing upon encountering a NULL character in a string?
  59. Why add a using statement and a reference?
  60. Why are hashtable lookups so slow with struct keys?
  61. Why do I get a syntax error when I try to declare a variable named checked?
  62. Why does C# not support checked exceptions?
  63. Why does my Windows application open a console window each time it is run?
  64. Why doesn't the C# switch statement work like I expect it to?
  65. Why doesn't a Windows Form project use visual styles in XP?
  66. Why get an error (CS1006) when declaring a method without a return type?
  67. Why must struct constructors have at least one argument?
C# Online.NET