You are able to set a default value for the b
member like this: int b = 100;
. However, after the member declaration, you won't be able to assign a value to b
member outside a function. When you want to assign a value to a member, you should determine when it should be done. So if you want to set the b
value when a instance of Similarity
is created, you can assign it in the class constructor:
int b;
public string index;
public double distance;
public double similarityRate;
public Similarity(string index, double distance)
{
this.index = index;
this.distance = distance;
b = 100;
}
On a side note, if you want to declare b
as a public property, the right way to do this would be public int b {get; set;}
. Since C# 6, you can also set a default value directly to the a property: public int b {get; set;} = 100;