c# - Constructor in Constructor -
is there way how can in c#? can not use this(int,int) in c# code. can write me similar code in c# , same things? thank you! :)
public class javaapplication2 { public static class someclass { private int x; private int y; public someclass () { this(90,90); } public someclass(int x, int y) { this.x = x; this.y = y; } public void showmevalues () { system.out.print(this.x); system.out.print(this.y); } } public static void main(string[] args) { someclass myclass = new someclass (); myclass.showmevalues(); } }
there several things need changed if want translate c#:
- the main problem you've declared
someclass
static. won't compile because static classes cannot have instance members. need removestatic
keyword. - to invoke constructor need use
: this(...)
after constructor parameters (or: base(...)
invoke constructor of parent class). - instead of
system.out
, .net applications need usesystem.console
.
this should work you:
public class someclass { private int x; private int y; public someclass () : this(90,90) { } public someclass(int x, int y) { this.x = x; this.y = y; } public void showmevalues () { console.writeline(this.x); console.writeline(this.y); } }
Comments
Post a Comment