Strange behavior of method overloading

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

Guest

Hi,
i found an interesting problem when using overloaded functions...
I declare two methods:

public static void ShowError(Exception exp) {...}
and
public static void ShowError(SqlCeException exp) {...}

when i call ShowError with parater of type SqlCeException, then compiler's
overload resulution determine to invoke ShowError(Exception) instead of
ShowError(SqlCeException)!

As written in C# specification 1.2, compiler should find the method that
BEST matches
arguments, should't it?
 
There's nothing strange about it. Your exception is declared as Exception,
so compiler chooses the first overload at compile time (early binding).
It would not care about actual type at runtime (late binding). Late binding
should be avoided anyway.

I would suggest getting rid of the second overload and checking for a type
of exception instead:

if (exp is SqlCeException) {
// SQL CE exception
}
else {
// Generic exception
}


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
I do not understand what you mean with "Your exception is declared as
Exception".
My exception is declared as SqlCeException witch is only inherited from
Exception type.

Mentioned behavior is strange -
YOU CAN NOT USE METHOD OVERLOADING THROUGH OBJECT HIERARCHY!


"Ilya Tumanov [MS]" said:
There's nothing strange about it. Your exception is declared as Exception,
so compiler chooses the first overload at compile time (early binding).
It would not care about actual type at runtime (late binding). Late binding
should be avoided anyway.

I would suggest getting rid of the second overload and checking for a type
of exception instead:

if (exp is SqlCeException) {
// SQL CE exception
}
else {
// Generic exception
}


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Strange behavior of method overloading
thread-index: AcUV4m4o7bkTy4RZQA+Zgy0VSAlxfg==
X-WBNR-Posting-Host: 194.228.214.242
From: =?Utf-8?B?TWljaGFsIFJpemVr?= <[email protected]>
Subject: Strange behavior of method overloading
Date: Fri, 18 Feb 2005 09:51:10 -0800
Lines: 16
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:22844
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Hi,
i found an interesting problem when using overloaded functions...
I declare two methods:

public static void ShowError(Exception exp) {...}
and
public static void ShowError(SqlCeException exp) {...}

when i call ShowError with parater of type SqlCeException, then compiler's
overload resulution determine to invoke ShowError(Exception) instead of
ShowError(SqlCeException)!

As written in C# specification 1.2, compiler should find the method that
BEST matches
arguments, should't it?
 
Allow me to demonstrate:

using System;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.Runtime.InteropServices;

namespace tests
{

public class Class1
{
public static void PrintEx (Exception e) {
Console.WriteLine ("Generic overload called for exception {0}", e);
}

public static void PrintEx (ArgumentException e) {
Console.WriteLine ("ArgumentException overload called for exception
{0}", e);
}


public static void Main()
{
Exception ge = new Exception ("Generic exception.");
ArgumentException ae = new ArgumentException ("ArgumentException
exception.");
Exception gea = new ArgumentException ("ArgumentException exception.");

PrintEx (ge);
PrintEx (ae);
PrintEx (gea);
}
}
}


That would produce the following output:

Generic overload called for exception System.Exception: Generic exception.
ArgumentException overload called for exception System.ArgumentException:
ArgumentException exception.
Generic overload called for exception System.ArgumentException:
ArgumentException exception.

Note correct overload is chosen in case #2 because exception is know to be
ArgumentException at compile time.
Generic overload is chosen in case #3 because exception is know to be
Exception at compile time and it's irrelevant what type it actually is at
runtime.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Strange behavior of method overloading
thread-index: AcUW17g5vQDJWO/ESeegEJmGxK9EUw==
X-WBNR-Posting-Host: 62.24.87.249
From: =?Utf-8?B?TWljaGFsIFJpemVr?= <[email protected]>
References: <[email protected]>
Subject: RE: Strange behavior of method overloading
Date: Sat, 19 Feb 2005 15:07:01 -0800
Lines: 80
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA
03.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:71517
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I do not understand what you mean with "Your exception is declared as
Exception".
My exception is declared as SqlCeException witch is only inherited from
Exception type.

Mentioned behavior is strange -
YOU CAN NOT USE METHOD OVERLOADING THROUGH OBJECT HIERARCHY!


"Ilya Tumanov [MS]" said:
There's nothing strange about it. Your exception is declared as Exception,
so compiler chooses the first overload at compile time (early binding).
It would not care about actual type at runtime (late binding). Late binding
should be avoided anyway.

I would suggest getting rid of the second overload and checking for a type
of exception instead:

if (exp is SqlCeException) {
// SQL CE exception
}
else {
// Generic exception
}


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Strange behavior of method overloading
thread-index: AcUV4m4o7bkTy4RZQA+Zgy0VSAlxfg==
X-WBNR-Posting-Host: 194.228.214.242
From: =?Utf-8?B?TWljaGFsIFJpemVr?=
Subject: Strange behavior of method overloading
Date: Fri, 18 Feb 2005 09:51:10 -0800
Lines: 16
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path:
TK2MSFTNGXA01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:22844
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Hi,
i found an interesting problem when using overloaded functions...
I declare two methods:

public static void ShowError(Exception exp) {...}
and
public static void ShowError(SqlCeException exp) {...}

when i call ShowError with parater of type SqlCeException, then compiler's
overload resulution determine to invoke ShowError(Exception) instead of
ShowError(SqlCeException)!

As written in C# specification 1.2, compiler should find the method that
BEST matches
arguments, should't it?
 
Back
Top