Home > Blockchain >  Setting a value to a parameter in the Constructor (C#)
Setting a value to a parameter in the Constructor (C#)

Time:05-21

Hello i was just wondering if this is valid

    public test( string name, double kpiScore, bool isPrivate = false)
    {

   

    this.name = name;

     
        this.kpiScore = score;


        this.isPrivate = isPrivate;

        

}

Can i set the bool isPrivate = false, in the parameter call? Or do i set it in the block itself. I've never seen it where the parameter is set within the parameter call. I've looked online and no one mentions this. -- this is the original uml diagram, i modified it just to make the problem simpler.

enter image description here

CodePudding user response:

There is a big difference between

public test( string name, double score, bool isPrivate = false)
{
   this.name = name;
   this.kpiScore = score;
   this.isPrivate = isPrivate;
}

and

public test( string name, double score)
{
   this.name = name;
   this.kpiScore = score;
   this.isPrivate = false;
}

in the first case the caller can do

var t = new test("x", 42);

or

var t = new test("x", 42, true);
    

ie they can choose to override the privacy option. In the second case they do not have that choice. So the answer depends on what your end goal is

  •  Tags:  
  • c#
  • Related