How to do constructor chaining in C#

前端 未结 8 1662
挽巷
挽巷 2020-11-22 16:25

I know that this is supposedly a super simple question, but I\'ve been struggling with the concept for some time now.

My question is, how do you chain constructors

8条回答
  •  星月不相逢
    2020-11-22 16:43

    I have a diary class and so i am not writing setting the values again and again

    public Diary() {
        this.Like = defaultLike;
        this.Dislike = defaultDislike;
    }
    
    public Diary(string title, string diary): this()
    {
        this.Title = title;
        this.DiaryText = diary;
    }
    
    public Diary(string title, string diary, string category): this(title, diary) {
        this.Category = category;
    }
    
    public Diary(int id, string title, string diary, string category)
        : this(title, diary, category)
    {
        this.DiaryID = id;
    }
    

提交回复
热议问题