C Sharp - Converting Data from one variable type to another

Convert a number into a null

command.Parameters.Add("@WfStepId", SqlDbType.BigInt).Value = wfStepId == 0 ? null : wfStepId;
            

If the value of wfStepId=0 then put a null in the value, otherwise pass across wfStepId

Convert a string to a number

string templateCount = "0";
if (int.Parse(templateCount) > 0)
{

Converting an object into a string.

Here are 2 examples

1
2
3
4
string outputFilePath = (string)configuration
.GetSection(Route.AppSettings.CalPERS457)
.GetSection(Route.AppSettings.FilePaths)
.GetValue(typeof(string), Route.AppSettings.OutputPath);

In this example the configuration is a IConfiguration object. I am looking to get the value of the OutputPath key. The GetValue method returns an object. So I am converting the object to a string.

Later on AI suggested the following


  // Get output file path
  string outputFilePath = configuration
            .GetSection(Route.AppSettings.CalPERS457)
            .GetSection(Route.AppSettings.FilePaths)
            .GetValue<string>(Route.AppSettings.OutputPath) ?? string.Empty ;

eliminates compiler warnings