-1

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.

Raghubar
  • 2,768
  • 1
  • 21
  • 31

1 Answers1

4

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...

CodeMan
  • 671
  • 6
  • 13