.net - Appeler le constructeur de base en C#

Mots clés : c#.netinheritanceconstructorc#

meilleur 5 Réponses .net - Appeler le constructeur de base en C#

vote vote

98

public class MyExceptionClass : Exception {     public MyExceptionClass(string message, string extrainfo) : base(message)     {         //other stuff here     } } 
vote vote

80

class MyExceptionClass : Exception {      public MyExceptionClass(string message, string extraInfo) :           base(ModifyMessage(message, extraInfo))      {      }       private static string ModifyMessage(string message, string extraInfo)      {          Trace.WriteLine("message was " + message);          return message.ToLowerInvariant() + Environment.NewLine + extraInfo;      } } 
vote vote

76

public class MyClass : BaseClass {     private MyClass(string someString) : base(someString)     {         //your code goes in here     }      public static MyClass FactoryMethod(string someString)     {         //whatever you want to do with your string before passing it in         return new MyClass(someString);     } } 
vote vote

60

public ClassName() : this(par1,par2) { // do not call the constructor it is called in the this. // the base key- word is used to call a inherited constructor    }   // Hint used overload as often as needed do not write the same code 2 or more times 
vote vote

52

public class MyExceptionClass : Exception {     public MyExceptionClass(string message,       Exception innerException): base(message, innerException)     {         //other stuff here     } } 

Questions similaires