简体   繁体   中英

How to create instance of class that have parameterized constructor

One of interviewer asked me this question: How to create instance of class that have parameterized constructor, but i will not send the parameter information. Example:

public class abc{
     public abc(string str)
     {
     }
}

Now if we create instance of it then i have to do it in this way:

abc abcInstance = new abc("xyz");

But he wants that he will not pass parameter eg.:

abc abcInstance = new abc();

Please help me out. Some one suggested Dependency Injection Please share an example. Thanks in advance.

string is nullable, but you can do this:

public class abc{
     public abc(anyType str = defaultvalue)
     {
     }
}

If anytype is int, use defaultvalue as 0 for example and etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM