Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Friday, December 30, 2005 - Posts

Generic Type Inference

One of the cooler features I like in Generics is type inference. Type inference allows you to call a generic method just as you would a non-generic method using its method signatures as references to the type(s), instead of specifying the type(s).

VB.NET
Public Sub TestMethod1( Of T, U)( ByVal val1 As T, ByVal val2 As U)
   Response.Write( "Value 1: {0}, Value 2: {1}" , val1, val2)
End Sub

Public Function  TestMethod2( Of T)( ByVal val As T) As T
    Return val
End Function

TestMethod1( "Bob" , 172)
' Same as TestMethod1(Of String, Integer)("Bob", 172)
TestMethod1(14, "Marry" )
' Same as TestMethod1(Of Integer, String)(14, "Marry")
TestMethod2(46)
' Same as TestMethod2(Of Integer)(46)

C#
public void TestMethod1(T val1, U val2)
{
   Response.Write( "Value 1: {0}, Value 2: {1}" , val1, val2);
}

public T TestMethod2(T val)
{
    return val;
}

TestMethod1( "Bob" , 172);
// Same as TestMethod1("Bob", 172)
TestMethod1(14, "Marry" );
// Same as TestMethod1(14, "Marry")
TestMethod2(46);
// Same as TestMethod2(46)

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 30 2005, 09:15 AM by Ryan Olshan | with no comments
Filed under:
More Posts