[C#] Optional parameters with Classes

Hi,
I want to white a method with an optional Vector3 parameter, so I’ve tried this but that don’t work:

public void DoSomething(Vector3 position = new Vector3(0, 0, 0))

Unity give me this error : “The expression being assigned to optional parameter `position’ must be a constant or default value”

Which syntax I should use, please ?

public void DoSomething(Vector3 position = default(Vector3))
3 Likes

Thanks alexzzzz, but that work only if I want 0, 0, 0 position.

How I can make an optional parameter like Vector3(10, 0, 5) ?

You can’t ― Named and Optional Arguments - C# | Microsoft Learn

Does method overloading do the job for you?

public void DoSomething(Vector3 position)
{
  // do sth.
}

public void DoSomething()
{
  DoSomething(new Vector3(10, 0, 5));
}

Yes, this can do, it’s unfortunate C# can’t handle this with parameters.
In any case, thank you both.

I can’t test this right now, but as the error message says “must be a constant or default value”, couldn’t you do something like this?

const Vector3 DOSOMETHING_DEFAULT = new Vector3(10, 0, 5);

public void DoSomething(Vector3 position = DOSOMETHING_DEFAULT);

This will not work, as this is not a compile time constant.

Compile time constants can only be for primitive types like enums, float, double, strings, eg.

To overcome this, you could as already said, create imposter by using overloading or do something like this:

public void DoSomething(Vector3? position = null)
{
  if (position == null)
    position = new Vector3(10, 0, 5);

  .. // do stuff
}
1 Like

A constant’s value must be known at compile time. “new Vector3(10, 0, 5)” implies that you have to run some code to get the value, but you can’t run the code that hasn’t been compiled yet.

in csharp u can not make optional parameter values.

you can override like

private void SomeMethod(string whatever)
{
SomeMethod(whatever, “someval”);
}

private void SomeMethod(string whatever, string whatever2)
{
//implement
}

or you have to prove the parameter’s value in your method.

It can, but only at C# 4.0 (Unity uses something below that).

Not sure about the OptionalAttribute and DefaultValueAttribute as they are listed even for .Net 1.1

Named and optional parameters are C# 4.0 feature, but VS2010 supports them even if you target .Net 3.5. Unity’s compiler also supports named and optional parameters.

yeah, .NET 4.0 … but Unity works with 2.0

You can use optional parameters in Unity. Really. I do.

For optional parameters, I think it only work with primitive datatypes. I remembered using it as well, for something like int input = 0, if my memory serves.

Named argument looks really charming. I love that in Objective-C and really miss it when I switch to C# and Unity. Hopefully Unity can switch to use .NET 4.0 soon

Unity’s Mono supports most of .Net Framework 3.5 libraries/classes, Common Language Runtime 2.0, and C# 4.0. You may use C# 4.0 as long as you do not use CLR 4.0 and .Net 4.0 features.

Both named and optional parameters are supported by Unity (I’ve only tested on Unity4), using VS2010.