What do we mean by Optional parameter in VB.Net and Params in C#/ParamArray in VB.Net.

 Posted by vishalneeraj-24503 on 11/25/2013 | Category: ASP.NET Interview questions | Views: 1304 | Points: 40

1). Both are defined as the Last arguments in Method definition.After that no any variables are defined.

2). We cannot use both Optional and ParamArray/Perams in the same parameter list.

3). Both are optional means we need not to pass values to them.

4). Optional parameters may not be defined as an array but ParamArray as the name implies, always defined as an Array.

4). We always assign default values to Optional parameters in method declaration whereas we do not require in case of ParamArray.

Example of Optional Parameters in VB.Net ->

Private Sub Update_Records(ByVal emp_id as Integer,Optional ByVal is_checked as Boolean = False)

'If(is_checked as Boolean) Then
'code for Update statement
'code for Insert statement
'End If
End Function

Example of ParamArray in VB.Net ->

Private Function Get_Values(ByVal emp_id as Integer,ByVal ParamArray list() As Integer) As Integer

If(Not list is Nothing) Then
For Each index In list
Console.WriteLine("Values are {0}", index)
End If

End Function

Example of Params in C# ->

private void Get_Value(params int[] list)

foreach(int value in list)
Console.WriteLine("Values are {0}", value)

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response