make that:
static void DoStuff(Type type, string input)
{
object value;
if (TryParse(input, out value))
{
// ...
}
}
On 4/11/08, Sébastien Lorion <[EMAIL PROTECTED]> wrote:
> Well, in my code I have another method similar to:
>
> void DoStuff(Type type, string input)
> {
> object value;
> if (TryParse(input, out value)
> {
> // ...
> }
> }
>
> I made a mistake and forgot to pass "type" to TryParse. I discovered
> the bug only at runtime when testing my code. I can understand the
> compiler behavior, but it is not what I expect since I think in most
> case, the programmer did not intend to do that. A warning would be
> nice here IMO.
>
> Sébastien
>
>
> On 4/11/08, Greg Young <[EMAIL PROTECTED]> wrote:
> > object value;
> > Parser.TryParse(typeof(int), "1", out value);
> >
> >
> > is a valid call to the second (non-generic method).
> >
> >
> > It would seem to me that it is then inferring T as object here ...
> >
> >
> > return TryParse(input, out value);
> >
> >
> > because if
> >
> > public static bool TryParse<T>(string input, out T value) value is
> > an object ... (and its a T) T must be an object.
> >
> >
> > What were you expecting to be the error and why?
> >
> > Cheers,
> >
> > Greg
> >
> >
> > On Fri, Apr 11, 2008 at 5:06 PM, Sébastien Lorion
> > <[EMAIL PROTECTED]> wrote:
> > > The following program outputs:
> > >
> > > in Parse, type='System.Int32'
> > > in Parse<T>, T='System.Object'
> > >
> > > Is this behavior intended and if yes, why ? I would expect a compile
> error.
> > >
> > >
> > >
> > > using System;
> > >
> > > namespace ConsoleApplication1
> > > {
> > > class Program
> > > {
> > > static void Main(string[] args)
> > > {
> > > object value;
> > > Parser.TryParse(typeof(int), "1", out value);
> > >
> > > Console.ReadLine();
> > > }
> > > }
> > >
> > > class Parser
> > > {
> > > public static bool TryParse<T>(string input, out T value)
> > > {
> > > Console.WriteLine("in Parse<T>, T='{0}'", typeof(T));
> > >
> > > value = default(T);
> > > return true;
> > > }
> > >
> > > public static bool TryParse(Type type, string input, out object
> value)
> > > {
> > > Console.WriteLine("in Parse, type='{0}'", type);
> > >
> > > return TryParse(input, out value);
> > > }
> > > }
> > > }
> > >
> > > --
> > > Sébastien
> > > www.sebastienlorion.com
> > >
> >
> > > ===================================
> > > This list is hosted by DevelopMentor(R) http://www.develop.com
> > >
> > > View archives and manage your subscription(s) at
> http://discuss.develop.com
> > >
> >
> >
> >
> >
> > --
> > Studying for the Turing test
> >
> > ===================================
> > This list is hosted by DevelopMentor(R) http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> http://discuss.develop.com
> >
>
>
> --
>
> Sébastien
> www.sebastienlorion.com
>
--
Sébastien
www.sebastienlorion.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com