How I check if optional parameters were passed to a c# method

Checking if Optional Parameters in C# were passed to your class or routine

I've read a few articles on this - specifically people asking how to check if an optional parameters has been passed to a C# method/function/class.

I saw several responses where people suggested you don't have to check - just set your default values or use overloading.

The problem with both solutions is that they fail to address the common situation of wanting a flexible method that only updates those parameters you actually pass.

Overloading does not allow you to determine which named parameters were actually passed. And, you do not want to write default values to a record if the values in the record are not meant to be changed.

I wanted a method that allows me to pass only those parameters for fields being updated but allows me to use the method universally whether a single field is updated or all fields are updated.

I've included the method and how it is called in the code below. It is contained in a class named "JobInfo" where I perform a number of actions/methods on the Job data.

Note: My actual method has 15 different fields. Using this method, I can use the same method to provide a way to update a single field after update. This comes up with job names, using ajax, a lot. My client can click on the job name in a list of jobs and edit it in place. The Job_ID and Jobname are the only parameters passed.

I would have to use a different default date if the application could possibly include dates far in the past. However, this is a project management/tracking application that will never have a job requested go that far back.

Also, I considered using the word "Null" as a way to specify whether a date should be set to a null value.

Your improvements/suggestions are appreciated.

Also, my syntax highlighter is acting oddly - so I apologize about how ugly the code looks. I don't have the time at the moment to work on this. Your understanding is appreciated. 😉


public string EditJob(int inJob_ID, int inProj_ID = -1, string inJobName = "-NA", string inReq_dt="8/24/410")
string cn = ConfigurationManager.ConnectionStrings["MYDB"].ConnectionString;
SqlConnection cxn = new SqlConnection(cn);

string strFldSQL = "";
if (inProj_ID != -1) { strFldSQL += "Project_ID = @ProjID"; }
if (inJobName != "-NA") { strFldSQL += ",JobName = @Jobname"; }
if (inReq_dt != "8/24/410") { strFldSQL += ",Requested_dt = @req_dt"; }

string schk = strFldSQL.Substring(0, 1);
if (schk == ",")
strFldSQL = strFldSQL.Substring(1, strFldSQL.Length - 1);

strFldSQL = "UPDATE TblJobs SET " + strFldSQL + " WHERE Job_ID = " + inJob_ID;

using (SqlCommand cmd = new SqlCommand(strFldSQL, cxn))
if (inProj_ID != -1) { cmd.Parameters.AddWithValue("@ProjID", inProj_ID); }
if (inJobName != "-NA") { cmd.Parameters.AddWithValue("@jobname", inJobName); }
if (inReq_dt != "8/24/410")
if (inReq_dt == "")
cmd.Parameters.AddWithValue("@Req_dt", DBNull.Value);
cmd.Parameters.AddWithValue("@req_dt", inReq_dt);

return "";


// calling the method
jobinfo ji = new jobinfo

// changes the job name and makes the requested date null for job 2
ji.EditJob(2, inJobName: "Requested made null", inReq_dt: "");
// edits the requested date but nothing else.
ji.EditJob(14, inReq_dt: "5/20/2019");
// edits the job name and nothing else
ji.EditJob(8, inJobName: "Our new job name");

Posted in Consulting, Programming, SQL Server, Tips and Tools and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *