How to write Python's super() call at the end of an __init__ block in C#? -
i've been porting lot of python code on c# , regularly come across super().__init__
call @ end of __init__
block in python. problem comes in when python code in derived classes __init__
block before calling base constructor. in c#, it's assumed base constructors required derived constructors work, , called first, before "stuff" done in derived constructor. usual workaround write static method , call inside of base (ex. public c : base( dostuff(somevariable) )
. worked okay when had 1 argument base constructors. if have 3 arguments? don't want repeat __init__
block code 3 times using 3 different static methods, instead wrote 1 method returns first argument , sets other arguments in local variables.
is there better way accomplish doing stuff two+ variables in derived constructor before passing results base constructor in c#?
here testing code wrote observe behavior of workaround:
using system; namespace basesupertest { class program { static void main(string[] args) { console.writeline("in main..."); var b = new b(0,0,"zero"); console.writeline(); var c = new c(); } } class { public a(int first, int second, string third) { console.writeline("\tin a's constructor..."); console.writeline("\t{0} {1} {2}", first, second, third); } } class b : { public static int second { get; set; } public static string third { get; set; } private static int dostuff(int first, int second, string third) { console.writeline("\t\tin dostuff()..."); second = second + 1; third = "changed"; return first + 1; } public b(int first, int second, string third) : base(dostuff(first, second, third), second, third) { console.writeline("\t\tin b's constructor..."); } } class c : b { public c() : base(0,0,"zero") { console.writeline("\t\t\tin c's constructor..."); } } }
Comments
Post a Comment