I want to define a function in c# with 2 parameters and they have their default values , like
public T AccessEntity(string Id = null, string File = null)
{
return (from e in ServiceContext.CreateQuery<T>(TableName)
where e.RowKey == Id || e.File == File
select e).FirstOrDefault();
}
Now with this function user can search their record by file or id but if user try to search the record by file then how we can map the first argument to second formal parameter without passing any dummy value as a first argument.
I do not want this : invokingobj.AccessEntity(null, "name of file");
Is this possible ?
You can use named arguments:
Note that you should rename your parameter to
filethough, to comply with normal .NET naming conventions (“do use camel casing in parameter names”).As an aside, almost everyone – including MSDN – gets confused about the names of the two defaults here. They are optional parameters (it’s the parameter which is decorated with the default value) but named arguments (parameters have always had names – it’s arguments that are allowed to specify a name in C# 4).