compile time casting guarantee

E

Eric Newton

I'm wondering if there's a compile time cast gaurantee if any?

Given the following:

//----- begin sample code
public interface IRegion
{
string Text { get; }
RectangleF Area { get; }
}

public class Region : IRegion
{
public Region()
{
}
public Region(string text)
{
this._text = text;
}
public string Text { get { return this._text; } } // takes care of
IRegion.Text
internal protected void Set_Text(string value) { this._text = value; }
public RectangleF Area { get { return RectangleF.Empty; } } //takes care
of IRegion.Area
}
//---- end of code

Since Region specifically implements IRegion, we are guaranteed that a cast
from Region to IRegion will work, so I'm hoping for a more strict compile
time casting check that would obviously fail if the Region class doesnt
implement IRegion anymore, instead of finding out at runtime that the cast
failed.

Are there any plans for this?
 
N

Nicholas Paldino [.NET/C# MVP]

Eric,

Unfortunately, no, there is no compile-time check to see if it
implements the interface. You will have to wait until runtime to find out
if the cast is valid.

Hope this helps.
 
B

Bruno Jouhier [MVP]

Let us consider the following

Region reg = new Region();
IRegion ireg = reg;

You don't need any cast on the second line if Region implements the IRegion
interface (*)

If Region did not implement IRegion, you whould need to write:

IRegion ireg = (IRegion)reg;

The compiler should generate an error if Region was a sealed class (I did
not check if it actually does this but a good compiler should), but
otherwise (Region not sealed), static type analysis is insufficient to
conclude.

(*) Would be nice if the compiler generated warning messages when it
encouters "superfluous" casts like in this case.

Bruno.
 
E

Eric Newton

Ok cool, I was overlooking a very simple solution

When Region directly implements IRegion then the compiler does in fact allow
it
and when region doesnt implement IRegion then the compiler throws an error
saying it cannot implicitly cast which is the correct operation...

I knew I had to be overlooking something obvious ;-)


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Bruno Jouhier said:
Let us consider the following

Region reg = new Region();
IRegion ireg = reg;

You don't need any cast on the second line if Region implements the IRegion
interface (*)

If Region did not implement IRegion, you whould need to write:

IRegion ireg = (IRegion)reg;

The compiler should generate an error if Region was a sealed class (I did
not check if it actually does this but a good compiler should), but
otherwise (Region not sealed), static type analysis is insufficient to
conclude.

(*) Would be nice if the compiler generated warning messages when it
encouters "superfluous" casts like in this case.

Bruno.

Eric Newton said:
I'm wondering if there's a compile time cast gaurantee if any?

Given the following:

//----- begin sample code
public interface IRegion
{
string Text { get; }
RectangleF Area { get; }
}

public class Region : IRegion
{
public Region()
{
}
public Region(string text)
{
this._text = text;
}
public string Text { get { return this._text; } } // takes care of
IRegion.Text
internal protected void Set_Text(string value) { this._text = value; }
public RectangleF Area { get { return RectangleF.Empty; } } //takes care
of IRegion.Area
}
//---- end of code

Since Region specifically implements IRegion, we are guaranteed that a cast
from Region to IRegion will work, so I'm hoping for a more strict compile
time casting check that would obviously fail if the Region class doesnt
implement IRegion anymore, instead of finding out at runtime that the cast
failed.

Are there any plans for this?

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 

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