C# FAQ: What is the difference between calling base class constructors in CSharp and Java
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
[edit]
What is the difference between calling base class constructors in C# and Java?
Both the C# and Java languages provide ways to call one constructor from another in a process called constructor chaining. This reduces constructor code duplication.
The following are C# and Java examples of constructor chaining with custom exceptions:
using System; class CSharpException: Exception { private int errorNumber; public CSharpException (string errorMessage): this (errorMessage, null, 99) {} public CSharpException (string errorMessage, Exception nestedException): this(errorMessage, nestedException, 99) {} public CSharpException (string errorMessage, Exception nestedException, int errorNumber): base (errorMessage, nestedException) { this.errorNumber = errorNumber; } }
class JavaException extends Exception { private int errorNumber; public JavaException (String errorMessage) { this (errorMessage, null, 99); } public JavaException (String errorMessage, Exception nestedException) { this (errorMessage, nestedException, 99); } public JavaException (String errorMessage, Exception nestedException, int errorNumber) { super (errorMessage, nestedException); this.errorNumber = errorNumber; } }
[edit]
See also
- C# for Java Developers
- Does C# support custom exceptions?
- What is the difference between C# and Java constructor chaining?
- What is the difference between calling base class constructors in C# and Java?
- Are C# constructors inherited?
- How can one constructor call another?
- Are constructors the same in C++ and C#?
- Are destructors the same in C++ and C#?
- How call a virtual method from a constructor or destructor?
- How do destructors work in C#?
- How enforce constructor correctness in C#?
- How make a C# destructor virtual?
- What are the differences between C# and Java constructors?
- What are the differences between C# finalizers and Java destructors?
- What is the syntax for calling an overloaded constructor from within a constructor?
- Why must
structconstructors have at least one argument?