Simple compile error in class constructor

I have a very simple class that wont compile because of a default parameter in the constructor. The language is C# and the class will be used in a Unity3d game. So this compile error is occurring in Unity's "IDE"

Compile Error:

Assets/Utilities/GenericClasses.cs(30,94): error CS1736: The expression being assigned to optional parameter `nName' must be a constant or default value

public class Element {
    public static readonly string NULL_NAME = "___NULL_NAME___";
    public enum elementType {E_ELEMENT, E_MODEL, E_VIEW, E_SUPER, E_ARC};
    private string name;
    private elementType type;

    // line 30 is below
    public Element(elementType nType=elementType.E_ELEMENT, string nName=NULL_NAME) {
        type = nType;
        name = nName;

What am I doing wrong?


You should use a constant value instead of a static readonly:

public const string NULL_NAME = "___NULL_NAME___";

The reason why your code doesn't compile is because you attempted to assign a non constant default value to the nName parameter in the constructor which is not supported.

NULL_NAME is not a constant, it is readonly.

A readonly variable can be initialized in the constructor so it is not known at compile time which is required for a default value for an optional parameter because the compiler will substitute the value where it is omitted in a call.

Try this one

public const string NULL_NAME = "___NULL_NAME___";

Need Your Help

How do I reduce the Socialite scope in Laravel 5?

laravel google-api laravel-5 laravel-socialite

I'm successfully using Laravel 5's socialite feature, but I feel that it's asking for more permissions than makes sense for the end user when they sign up. For instance, Google tells my users that ...

Powershell: How to pass variable correctly to command

powershell activesync ems

I apologize if I butchered the terminology for this, and understand I'm very new to PowerShell. I have read over some of the guides and this concept is clearly not getting through to me.