Give "this" as the default value of the class method

I want to use this as the default value of a 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 enter me this error:

The default value for the "article" must be a compile-time constant.

Why does this error occur and how to fix it?

+4
source share
1 answer

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

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

(Note also that all parameters other than the default parameters should appear before any default ones.)

+12
source

Source: https://habr.com/ru/post/1478910/


All Articles