IStateManager Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a question pertaining to the implementation of an interface's
contract, specifically why in some interfaces the InterfaceName.method is
required when other times just the Interfaces method name will work. For
example,

When using ICollection.CopyTo Method I can implement the CopyTo method as
follows (notice the interface name is omitted):

public void CopyTo(Array array, int index)
{
//.....
}

But when using IStateManager.LoadViewState method, I must specify the
Interface name followed by .MethodName (otherwise will receive a compilation
error).

void IStateManager.LoadViewState(object savedState)
{
//...
}


I am fairly new to Interfaces and would like clarification on this topic.

Thanks
 
Chris,

Interfaces can be implemented in one of two ways, implicitly or
explicitly.

When implementing implicitly, then you don't need the interface name,
you just need a public method which has the same signature and name as the
method on the interface.

When implementing explicitly, you have no access modifier (as the access
is determined by the access modifier of the interface). When doing that,
you leave off the access modifier, and prefix the method name with the
interface name (and the period delimiter).

The reason you are getting the error is probably because your signature
is wrong, or it conflicts with something else on your class.

Hope this helps.
 
Thank you, that makes sense.

When is declaring an interface method implicitly vs explicitly important?
Why not always declare then implicitly?....

Nicholas Paldino said:
Chris,

Interfaces can be implemented in one of two ways, implicitly or
explicitly.

When implementing implicitly, then you don't need the interface name,
you just need a public method which has the same signature and name as the
method on the interface.

When implementing explicitly, you have no access modifier (as the access
is determined by the access modifier of the interface). When doing that,
you leave off the access modifier, and prefix the method name with the
interface name (and the period delimiter).

The reason you are getting the error is probably because your signature
is wrong, or it conflicts with something else on your class.

Hope this helps.


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

Chris Fink said:
I have a question pertaining to the implementation of an interface's
contract, specifically why in some interfaces the InterfaceName.method is
required when other times just the Interfaces method name will work. For
example,

When using ICollection.CopyTo Method I can implement the CopyTo method as
follows (notice the interface name is omitted):

public void CopyTo(Array array, int index)
{
//.....
}

But when using IStateManager.LoadViewState method, I must specify the
Interface name followed by .MethodName (otherwise will receive a
compilation
error).

void IStateManager.LoadViewState(object savedState)
{
//...
}


I am fairly new to Interfaces and would like clarification on this topic.

Thanks
 
Chris,

You might declare it explicitly when you don't want to make a method
publically accessible, but only through the interface implementation.


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

Chris Fink said:
Thank you, that makes sense.

When is declaring an interface method implicitly vs explicitly important?
Why not always declare then implicitly?....

Nicholas Paldino said:
Chris,

Interfaces can be implemented in one of two ways, implicitly or
explicitly.

When implementing implicitly, then you don't need the interface name,
you just need a public method which has the same signature and name as
the
method on the interface.

When implementing explicitly, you have no access modifier (as the
access
is determined by the access modifier of the interface). When doing that,
you leave off the access modifier, and prefix the method name with the
interface name (and the period delimiter).

The reason you are getting the error is probably because your
signature
is wrong, or it conflicts with something else on your class.

Hope this helps.


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

Chris Fink said:
I have a question pertaining to the implementation of an interface's
contract, specifically why in some interfaces the InterfaceName.method
is
required when other times just the Interfaces method name will work.
For
example,

When using ICollection.CopyTo Method I can implement the CopyTo method
as
follows (notice the interface name is omitted):

public void CopyTo(Array array, int index)
{
//.....
}

But when using IStateManager.LoadViewState method, I must specify the
Interface name followed by .MethodName (otherwise will receive a
compilation
error).

void IStateManager.LoadViewState(object savedState)
{
//...
}


I am fairly new to Interfaces and would like clarification on this
topic.

Thanks
 
Back
Top