I’m trying to convert come vb.net code into C# but came across optional in one of the functions.
Private Function doOpenConnection(ByRef cn As OleDb.OleDbConnection, ByRef cmd As OleDb.OleDbCommand, ByVal sConnString As String, Optional ByVal sUSP As String = "") As Boolean
It seems like instead of using overloading, VB.Net has an option to create it into one method/function. Does C# have a similar equilvalent or do I have to create a method for each possbility?
C# has an equivalent as of C# 4:
Note that you probably don’t need
reffor the first two parameters here – it’s important that you understand how parameter passing works in C#.C# 4 has both named arguments and optional parameters. See MSDN for more information. Note that there are various restrictions, in that optional parameters have to come before required ones (aside from parameter arrays), and the default value has to be a constant (or you can use the
default(...)operator).