Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Lambda and generics function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LiquidNitrogen - 09 Mar 2008 08:17 GMT
Hi guys,

As a new .net generics learner could some one care to explain what is
the difference when i declare the function such as

void function(Action<String> customAction)
{
  customAction("rohit");
}

and
void function<String>(Action<String> customAction)
{
  customAction("rohit");
}

The below sample program compiles fine but the moment I change Callme
to Callme<String> the compilers throws cannot convert from type
'string' to 'String'.

using System;
using System.Linq;
using System.Linq.Expressions;

public class test
{
   public void Callme(Action<String> action)
   {
       foreach (String name in names)
       {
           action(name);
       }
   }

   public void func(String name)
   {
       System.Console.WriteLine(name);
   }

   String[] names = { "Rohit", "Sharma" };
   public string name = "Rohit";
}

public class mainclass
{
   public static void Main()
   {
       test obj = new test();
       Action<String> ca = obj.func;

       obj.Callme(p => ca(p));
       Console.ReadKey();
   }
}
Jon Skeet [C# MVP] - 09 Mar 2008 08:42 GMT
> As a new .net generics learner could some one care to explain what is
> the difference when i declare the function such as
[quoted text clipped - 9 lines]
>    customAction("rohit");
> }

Well, the latter is trying to create a generic method with a type
parameter called String. It's as if you'd written:

void function<T>(Action<T> customAction)
{
   customAction("rohit");
}

The compiler is complaining because it can't convert from a string to a
T, and customAction takes a T not a string.

I suspect this isn't what you meant to do.

What were you trying to achieve by adding the <String> part?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.