473,654 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Selection when Using Parameterized Types (Generics)

I'm confused about the program below. Based on my reading of the C# spec, I
don't think it should compile, but it does when using Beta 1. Could somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
=============== =============== =============== ======
#region Using directives

using System;
using System.Collecti ons.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloa ds {
static void Main(string[] args) {
FunctionOverloa ds1<ClassA> x = new FunctionOverloa ds1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLin e();
}
public class ClassA {
}
public class FunctionOverloa ds1<typeParam> {

public void Function1(typeP aram arg) {
Console.WriteLi ne("Function1(t ypeParam arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'");
}
public void Function1(Class A arg) {
Console.WriteLi ne("Function1(C lassA arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'");
}

}
}
}
Nov 16 '05 #1
4 1383
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

"David Douglass" <Da***********@ discussions.mic rosoft.com> wrote in message
news:0D******** *************** ***********@mic rosoft.com...
I'm confused about the program below. Based on my reading of the C# spec, I don't think it should compile, but it does when using Beta 1. Could somebody please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
=============== =============== =============== ======
#region Using directives

using System;
using System.Collecti ons.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloa ds {
static void Main(string[] args) {
FunctionOverloa ds1<ClassA> x = new FunctionOverloa ds1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLin e();
}
public class ClassA {
}
public class FunctionOverloa ds1<typeParam> {

public void Function1(typeP aram arg) {
Console.WriteLi ne("Function1(t ypeParam arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'");
}
public void Function1(Class A arg) {
Console.WriteLi ne("Function1(C lassA arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'");
}

}
}
}

Nov 16 '05 #2
The program as written calls the function that takes ClassA as an argument.
But why not call the other signature that takes the type parameter? If the
function that takes ClassA as an argument is commented out, the other
function is called. Based on my reading of 20.1.8 of the C# Language
Specification, this seems ambiguous. Isn't this the same as F1 example in
20.1.8?

Thanks,
Dave

"Nick Malik" wrote:
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

"David Douglass" <Da***********@ discussions.mic rosoft.com> wrote in message
news:0D******** *************** ***********@mic rosoft.com...
I'm confused about the program below. Based on my reading of the C# spec,

I
don't think it should compile, but it does when using Beta 1. Could

somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
=============== =============== =============== ======
#region Using directives

using System;
using System.Collecti ons.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloa ds {
static void Main(string[] args) {
FunctionOverloa ds1<ClassA> x = new FunctionOverloa ds1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLin e();
}
public class ClassA {
}
public class FunctionOverloa ds1<typeParam> {

public void Function1(typeP aram arg) {
Console.WriteLi ne("Function1(t ypeParam arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'");
}
public void Function1(Class A arg) {
Console.WriteLi ne("Function1(C lassA arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'");
}

}
}
}


Nov 16 '05 #3
Good catch Dave. I believe that you are correct. The code below should
throw a compiler error... According to the spec, it should not compile.

The good thing is that it calls the correct method. The method defined as
"Function1(type Param arg)" should not be called as long as the other one is
defined with the same method signature. As you demonstrated, it is never
called as long as the other one is defined and the parameter type is of the
same type.

To be honest, there is power in the way that this is defined. It allows the
author of the generic definition to place a "type-specific" implementation
of "Function1" into the class, and allow a "catch-all" method to handle any
new or unexpected types. It is also, in all liklihood, encouraging sloppy
coding practices. Powerful but sloppy. That's a compelling combination,
huh?

If anyone has any further insight, I join Dave in asking that you chime in.

--- Nick

"David Douglass" <Da***********@ discussions.mic rosoft.com> wrote in message
news:95******** *************** ***********@mic rosoft.com...
The program as written calls the function that takes ClassA as an argument. But why not call the other signature that takes the type parameter? If the function that takes ClassA as an argument is commented out, the other
function is called. Based on my reading of 20.1.8 of the C# Language
Specification, this seems ambiguous. Isn't this the same as F1 example in
20.1.8?

Thanks,
Dave

"Nick Malik" wrote:
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

"David Douglass" <Da***********@ discussions.mic rosoft.com> wrote in message news:0D******** *************** ***********@mic rosoft.com...
I'm confused about the program below. Based on my reading of the C# spec,
I
don't think it should compile, but it does when using Beta 1. Could

somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
=============== =============== =============== ======
#region Using directives

using System;
using System.Collecti ons.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloa ds {
static void Main(string[] args) {
FunctionOverloa ds1<ClassA> x = new FunctionOverloa ds1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLin e();
}
public class ClassA {
}
public class FunctionOverloa ds1<typeParam> {

public void Function1(typeP aram arg) {
Console.WriteLi ne("Function1(t ypeParam arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() +

"'"); }
public void Function1(Class A arg) {
Console.WriteLi ne("Function1(C lassA arg)");
Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'"); }

}
}
}


Nov 16 '05 #4
I can't really accept this. Developing a language like C# isn't a corporate
IT project where you make it up as you go along. I'm going to consider this
a bug in beta 1 and submit it to Microsoft. At a minimum it's a bug in the
spec.

Can anybody from Microsoft chime in on this?

Dave

"Nick Malik" wrote:
Good catch Dave. I believe that you are correct. The code below should
throw a compiler error... According to the spec, it should not compile.

The good thing is that it calls the correct method. The method defined as
"Function1(type Param arg)" should not be called as long as the other one is
defined with the same method signature. As you demonstrated, it is never
called as long as the other one is defined and the parameter type is of the
same type.

To be honest, there is power in the way that this is defined. It allows the
author of the generic definition to place a "type-specific" implementation
of "Function1" into the class, and allow a "catch-all" method to handle any
new or unexpected types. It is also, in all liklihood, encouraging sloppy
coding practices. Powerful but sloppy. That's a compelling combination,
huh?

If anyone has any further insight, I join Dave in asking that you chime in.

--- Nick

"David Douglass" <Da***********@ discussions.mic rosoft.com> wrote in message
news:95******** *************** ***********@mic rosoft.com...
The program as written calls the function that takes ClassA as an

argument.
But why not call the other signature that takes the type parameter? If

the
function that takes ClassA as an argument is commented out, the other
function is called. Based on my reading of 20.1.8 of the C# Language
Specification, this seems ambiguous. Isn't this the same as F1 example in
20.1.8?

Thanks,
Dave

"Nick Malik" wrote:
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

"David Douglass" <Da***********@ discussions.mic rosoft.com> wrote in message news:0D******** *************** ***********@mic rosoft.com...
> I'm confused about the program below. Based on my reading of the C# spec, I
> don't think it should compile, but it does when using Beta 1. Could
somebody
> please explain the function selection rules to me?
>
> Thanks,
> --
> David Douglass
> MCSD for Microsoft .NET
> =============== =============== =============== ======
> #region Using directives
>
> using System;
> using System.Collecti ons.Generic;
> using System.Text;
>
> #endregion
>
> namespace CSharpSamples {
> class FunctionOverloa ds {
> static void Main(string[] args) {
> FunctionOverloa ds1<ClassA> x = new FunctionOverloa ds1<ClassA>();
> ClassA A = new ClassA();
> x.Function1(A);
> Console.ReadLin e();
> }
> public class ClassA {
> }
> public class FunctionOverloa ds1<typeParam> {
>
> public void Function1(typeP aram arg) {
> Console.WriteLi ne("Function1(t ypeParam arg)");
> Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'"); > }
>
>
> public void Function1(Class A arg) {
> Console.WriteLi ne("Function1(C lassA arg)");
> Console.WriteLi ne("arg.GetType () = '" + arg.GetType().T oString() + "'"); > }
>
> }
> }
> }
>
>


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3958
by: Abhishek Saksena | last post by:
Is it possible using Boost mpl library:- Assume any class implementing a function "connect" with two arugments of fixed types class protocol1 { connect(T0 & t0, T1 &t1 ){..} //fixed types T0 and T1 };
4
2028
by: Robbie Hatley | last post by:
I've been playing with "auto_ptr" and the "Resource Acquisition Is Initialization" concept. On page 199 of Lippman's book "Effective C++" he says "All active local class objects of a function are guaranteed to have their destructors applied before termination of the function by the exception handling mechanism.". I don't think that's true, though. The standard doesn't seem to require it. Section 15.2 just says that all objects...
3
1541
by: Sankar Nemani | last post by:
Hi, Does anyone know what the reason behind not allowing to use "covariant return types when overriding methods in derived classes" in C# is? Also while other OO languages such as Java, don't allow todo the same, why does C++ allow it and then the C# and VB.NET don't allow? I just want to understand the logical reasoning behind this decision if there is one.
24
2609
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
3
29040
by: John A Grandy | last post by:
does .NET have a Max() function ? something like MyMaxValue = Max(Value1,Value2,Value3,Value4)
6
4997
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: Prototypes are only good for headers to be included in other files. For functions which call each other inside one file, such as A calls B which calls C and D, just define the functions in order D, C, B, A, and you'll never need prototypes.
8
5308
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
9
1735
by: Ken Fine | last post by:
Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a test for meaningless <=180 as a boolean test). The function is designed to return values that will avoid positioning things a) upside down and b) at "too steep" an angle. I would now like to know how I could write my function so that the...
3
2410
by: Tony Johansson | last post by:
Hello! Is it possible to declare existing .net generics to have nullable types(for example type int? ) ? If the answer on the previous question is yes then I assume that you can also define my own generics class having nullable types. //Tony
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.