Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to skip parameters with default values?

Consider the following method example:

public void MyMethod (string par1, bool par2 = "true", string par3="")
{
}

Now let's say that I call MyMethod and set par3's value to "IamString".

How could I do that without setting par2's value to true or false?

I basically want to leave par2 value to its default.

I'm asking this because in Flash's ActionScript it is possible to do that by using the keyword default so I could call MyMethod ("somestring", default, "IamString") and par2 would be interpreted as true, which is its default value. I wonder if it is possible in C# as well.

like image 318
IneedHelp Avatar asked Aug 10 '12 12:08

IneedHelp


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What are basics of C?

It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.


1 Answers

You can specify this by name the parameter:

instance.MyMethod( "Hello", par3:"bla" );

Have a look here.

And there is another bug:

bool par2 = true

is correct..

like image 100
tuxtimo Avatar answered Oct 23 '22 16:10

tuxtimo