Quick (newbie) question on C# interfaces

P

Pritcham

Hi all

Apologies if this is a stupid question as I'm not a C# person but I've
seen some C# code where in the interface definition a property
(EntityState) is defined as readonly (EntityState EntityState {get;})
but when it comes to the base class implementation it's implemented as
read/write (i.e has a getter and setter)- is this normal in C#?

Again, apologies if it's a dumb question, just being nosey and trying
to learn something!

Cheers
Martin
 
N

Nicholas Paldino [.NET/C# MVP]

Martin,

That's a decision on the part of the implementer of the interface.
However, if you only have a reference to the interface definition, you will
NOT be able to set the value of the property.

Hope this helps.
 
N

Noah Sham

I don't know if its 'Normal' but the interface represents a contract and
that contract states that the property is read-only. There may be a need
when working with the class instance that you would want to have get/set
access to property value.
 
P

Pritcham

Hi Nicholas

Thanks for that - to be honest I do have control over the interface
definition as well but not being that familiar with C# I didn't know
whether it was common practice to not only implement the (required)
interface methods/properties but also have the ability to change their
definition but you've pretty much answered that for me so thanks.

Cheers
Martin said:
Martin,

That's a decision on the part of the implementer of the interface.
However, if you only have a reference to the interface definition, you will
NOT be able to set the value of the property.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Pritcham said:
Hi all

Apologies if this is a stupid question as I'm not a C# person but I've
seen some C# code where in the interface definition a property
(EntityState) is defined as readonly (EntityState EntityState {get;})
but when it comes to the base class implementation it's implemented as
read/write (i.e has a getter and setter)- is this normal in C#?

Again, apologies if it's a dumb question, just being nosey and trying
to learn something!

Cheers
Martin
 
P

Pritcham

Hi Noah

That would have been my assumption as well - as I say, I'm no C# person
so just wanted to make sure I wasn't misreading the
interface/implementation - looks like the interface definition needs a
small rewrite.

Thanks for replying so promptly!
Martin
 
S

sloan

interface IAnimal
{
string Name ( get; }


class Frog : IAnimal
{
public string Name()
{
get { return "I'm a Frog"} ;
set { Console.Writeline (value) ; }

}


}


In the case of this.

IAnimal fi = new Frog();
Console.Writeline( fi.Name );

you can only read the property.

Frog f = new Frog();
f.Name = "Youre a Frog";

here you can set the name. because you're not dealing with it as an
interface, but rather the concrete class.

"Normal".. I don't know. But its possible.
And actually a very subtle difference between c# and vb.net ...... one that
I like having.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top