harshil
harshil

Reputation: 9

Make argument compulsory or optional base on calling method

I have a this method

public void downloadAndSendFile(string fileType,
                                bool IsSendMail = false, 
                                string toEmail = "", 
                                string subject = "")
{
}

I want to do this,

Upvotes: 0

Views: 61

Answers (2)

주태민
주태민

Reputation: 1

 public void downloadAndSendFile(string fileType,
                            bool IsSendMail, 
                            string toEmail, 
                            string subject)
{
  if(IsSendMail)
  {
      ... code ... 
    Console.WriteLine(toEmail + subject);
  }
  else
 {
      ... code ... 
  }
}

Upvotes: 0

Sweeper
Sweeper

Reputation: 271040

What about using 2 overloads of the method instead?

public void downloadAndSendFile(string fileType)

and

public void downloadAndSendFile(string fileType,
                            string toEmail, 
                            string subject)

My reasoning behind this is that you don't need the IsSendMail parameter at all. All it indicates is whether there will be 2 more arguments in the method call. If that's all it does, why not remove it and do different things depending on the parameters passed in instead? If you need the value of IsSendMail inside the method, you can assume that it is true in the second overload, and false in the first overload.

Upvotes: 2

Related Questions