By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,952 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,952 IT Pros & Developers. It's quick & easy.

compile time casting guarantee

P: n/a
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/
er**@cc.ensoft-software.com [remove the first "CC."]
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric Newton" <er**@cc.ensoft-software.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

Nov 15 '05 #2

P: n/a
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" <er**@cc.ensoft-software.com> a écrit dans le message de
news:uf**************@TK2MSFTNGP10.phx.gbl...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

Nov 15 '05 #3

P: n/a
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <er**@cc.ensoft-software.com> a écrit dans le message de
news:uf**************@TK2MSFTNGP10.phx.gbl...
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/
er**@cc.ensoft-software.com [remove the first "CC."]


Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.