Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give "this" as default value of class method [duplicate]

Tags:

c#

I want to use this as default value of class method as this code:

public class Article
 {
    public int Id;//PK
    public String Author;//can be empty=anonymous
    public int? ToPublishDate;
    public String Summery;
    public String Content;
    public int RegDate;
    public Boolean Publish;

    private Boolean write(Article article=this,long position)
    {
        return true;
    }
 }

but on this give me this error:

Default parameter value for 'article' must be compile-time constant.

Why this error occurs and how can I fix it?

like image 529
Majid Avatar asked Dec 16 '22 11:12

Majid


1 Answers

You could set the default to null, and then reset its default in the method:

private Boolean write(long position, Article article=null)
{
    article = article ?? this;
}

(Note also that all non-default parameters have to come before any default ones.)

like image 131
McGarnagle Avatar answered Jan 18 '23 09:01

McGarnagle