Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# overloading and C++ templates

Status
Not open for further replies.

Larsson

Programmer
Jan 31, 2002
140
SE
Hi!

I wonder if there is something in C# like C++ templates from STL.
With a template you could do a swap function that could swap any two variables no matter what type they are, so long that they are the same type.

Template T<something don’t remember>

void swap(T &y, T &x)
{
T temp;
temp = x;
x = y;
y = temp;
}

int a, b;
double c, d;
bool e, f;

swap(a, b);
swap(c, d);
swap(e, f);

Can this be done in C#?

Larsson
 
Not really - you could fake it (like you say) with function overloading. But you'd have to write an overload for each potential type you'd pass, and that's a huge number of overloads.

Chip H.
 
I had an idea that it would be possible if I use something like this:

void swap(Object x, Object y)
{
Object temp;
temp = x;
y = x;
x = temp;
}

Would this work with:

int a, b;

swap (a, b);

Or would I have to box the integers first and how about other classes?

Larsson
 
I think an int (and other value types) would automatically be boxed in the call to the swap function. But inside the swap function, when you go to assign from the temp variable to one of the Object variables passed in, I think some unboxing would be required (supplied by you).

I dunno - why not give it a try with some different variable types, and let us know what works?

Chip H.
 
Well, I tried and here is the result. Not so good I'm afraid.

using System;

public class SomeClass
{
public static void swap (ref object x, ref object y)
{
object temp;
temp = x;
x = y;
y = x;
}

public static void main()
{
int a = 5, b = 10;
string c = &quot;Hello &quot;, d = &quot;World!&quot;;
TheClass e = new TheClass(5), f = new TheClass(10);

Console.WriteLine(&quot;int a = {0}, b = {1}&quot;, a, b);
Console.WriteLine(&quot;string c = {0}, d = {1}&quot;, c, d);
Console.WriteLine(&quot;TheClass e = {0}, f = {1}&quot;, e, f);

swap (ref a, ref b);
swap (ref c, ref d);
swap (ref e, ref f);

Console.WriteLine(&quot;After swap of a and b, c and d, e and f&quot;);
Console.WriteLine(&quot;int a = {0}, b = {1}&quot;, a, b);
Console.WriteLine(&quot;string c = {0}, d = {1}&quot;, c, d);
Console.WriteLine(&quot;TheClass e = {0}, f = {1}&quot;, e, f);
}
}

public class TheClass
{
public int x;
public TheClass(int y)
{
x = y;
}
}

C:\Arbetskatalog>csc hello.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

hello.cs(23,3): error CS1502: The best overloaded method match for
'SomeClass.swap(ref object, ref object)' has some invalid arguments
hello.cs(23,13): error CS1503: Argument '1': cannot convert from 'ref int' to
'ref object'
hello.cs(23,20): error CS1503: Argument '2': cannot convert from 'ref int' to
'ref object'
hello.cs(24,3): error CS1502: The best overloaded method match for
'SomeClass.swap(ref object, ref object)' has some invalid arguments
hello.cs(24,13): error CS1503: Argument '1': cannot convert from 'ref string' to
'ref object'
hello.cs(24,20): error CS1503: Argument '2': cannot convert from 'ref string' to
'ref object'
hello.cs(25,3): error CS1502: The best overloaded method match for
'SomeClass.swap(ref object, ref object)' has some invalid arguments
hello.cs(25,13): error CS1503: Argument '1': cannot convert from 'ref TheClass'
to 'ref object'
hello.cs(25,20): error CS1503: Argument '2': cannot convert from 'ref TheClass'
to 'ref object'

C:\Arbetskatalog>
 
Yes, it doesn't look good....

So I would say you can only simulate Templates by providing overloaded functions for each type and combination of datatypes you expect to be used.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top