Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To pass Optional Arguments

I have one function having two fixed arguments. But next arguments are not fixed, there can be two or three or four of them.

It's a runtime arguments so how can I define that function?

My code looks like:

public ObservableCollection<ERCErrors> ErrorCollectionWithValue
    (string ErrorDode, int MulCopyNo, dynamic arguments comming it should be 2 or 3)
        {

        return null;
    }
like image 268
Dhaval Patel Avatar asked Sep 25 '12 07:09

Dhaval Patel


People also ask

How do you pass optional arguments in TypeScript?

Use a question mark to set an optional parameter in a function in TypeScript, e.g. function sum(a: number, b?: number) {} . If set to optional, the parameter can have a type of undefined or the specified type, because unspecified parameters get the value undefined . Copied!

How do you pass optional parameters in darts?

A parameter wrapped by { } is a named optional parameter. Also, it is necessary for you to use the name of the parameter if you want to pass your argument.


1 Answers

1) params (C# Reference)

public ObservableCollection<ERCErrors>ErrorCollectionWithValue
                (string ErrorDode, int MulCopyNo, params object[] args)
{
    //...
}

2) Named and Optional Arguments (C# Programming Guide)

public ObservableCollection<ERCErrors> ErrorCollectionWithValue
    (string ErrorDode, int MulCopyNo, object arg1 = null, int arg2 = int.MinValue)
{
    //...
}

3) And maybe simple method overloading would still suit better, separating method logic to different methods? Under this link you can also find another description of named and optional parameters

like image 90
horgh Avatar answered Sep 19 '22 16:09

horgh