i’m learning C#.
ok, i have a problem:
i have a class :
class Couple{
private double first{private set; public get;}
private double second{private set; public get;}
}
first question: am i right, that these properities have public getter and private setter? (it’s sounds strange, i know, but need to know difference between private/public field and private/public property with public/private set/get )
and second question.
if i want a class :
class AnyCouple{
public Type AnyCouple {public set; public get;}
private AnyCouple first{private set; public get;}
private AnyCouple second{private set; public get;}
}
how to make it?
dummy questions, i know, sorry
For your first question, yes, you are right. The properties
firstandsecondhave a private setter and a public getter. However, as written, your code won’t compile. If you specify an access modifier on a getter or setter it must be more restrictive than the access modifier for the property and you can’t specify an access modifier on both properties. Additionally, note that in idiomatics C#, we write the getter first and setter second and we give properties PascalCase names likeThis will achieve a property named
Firstwith apublicgetter and aprivatesetter and it is written idiomatically.For your second question, you should use generics. You could do it all like this:
But, this is already built in to the .NET Framework. You can just use
Tuple<T1, T2>. Note that itsItem1andItem2properties (analogous to yourfirstandsecond) have a public getter; it is backed by aprivate readonlyfield.