We recommend using Visual Studio 2017

Generics FAQ: Tool Support

 

Juval Lowy

October 2005

Applies to:
   Generic Types
   Microsoft Visual Studio 2005

Summary: Review frequently asked questions regarding generic types and their various uses. (5 printed pages)

Contents

How Does Visual Studio 2005 Support Generics?
Can I Data-Bind Generic Types to Windows and Web Data Controls?
How Are Web Service Proxies Created for Generic Types?

How Does Visual Studio 2005 Support Generics?

Visual Studio 2005 supports generics well. InteliSense displays correctly the generic types, implementing generic interfaces is just as easy as with non-generic interfaces. The most impressive aspect of support is in the debugger, which displays the correct type arguments information when hovering over generic types.

Can I Data-Bind Generic Types to Windows and Web Data Controls?

Yes. All the generic collections also support the non-generic collection interfaces, and you can use them as data sources to bind to controls just as with the non-generics collections.

For example, consider a Windows Forms form that has a combobox called m_ComboBox. You can assign into as a data source the List<T> collection:

[C#]

partial class MyForm : Form
{
   void OnFormLoad(object sender, EventArgs e)
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisico");
      cities.Add("London");

      m_ComboBox.DataSource = cities;
   }
}

[Visual Basic]

Public Class MyForm 
   Inherits Form
   Private Sub OnFormLoad(ByVal sender As System.Object,
                       ByVal e As System.EventArgs) Handles MyBase.Load

      Dim cities As New List(Of String)
      cities.Add("New York")
      cities.Add("San Francisico")
      cities.Add("London")
      m_ComboBox.DataSource = cities
   End Sub
End Class

[C++]

public ref class MyForm : public Form
{
   void Form_Load(Object^ sender,EventArgs^ e)
   {
      List<String ^> ^cities = gcnew List<String ^>;
     cities->Add("New York");
     cities->Add("San Francisico");
     cities->Add("London");
     m_ComboBox->DataSource = cities;
   }
};

How Are Web Service Proxies Created for Generic Types?

The web service proxy class generated by Visual Studio 2005 does not necessarily maintain affinity to the returned types from a web service. The proxy class will contain values corresponding only to the serialized representation of the generic types only.

As mentioned in the question on generics and web services, for this definition of a web service:

[C#]

public class MyWebService 
{
   [WebMethod]
   public List<string> GetCities() 
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisco");
      cities.Add("London");
      return cities;
   }
}

[Visual Basic]

Public Class MyWebService
    [WebMethod]
    Public Function GetCities() As List(Of String)
        Dim cities As New List(Of String)()
        cities.add("New York")
        Cities.add("San Francisco")
        cities.add("London")
        Return cities
    End Function
End Class

[C++]

public ref class MyWebService 
{
   public:
   [WebMethod]
   List<String ^> ^ GetCities() 
   {
      List<String ^> ^cities = gcnew List<String ^>();
      cities->Add("New York");
      cities->Add("San Francisco");
      cities->Add("London");
      return cities;
   }
}

The returned list will be marshaled as an array of strings. Consequently, the Visual Studio 2005 generated proxy will contain this definition of the GetCities() method:

[C#]

[WebServiceBinding(Name="MyWebServiceSoap")]
public partial class MyWebService : SoapHttpClientProtocol 
{
   public MyWebService() 
   {...}

   [SoapDocumentMethod(...)]
   public string[] GetCities()
   {
      object[] results = Invoke("GetCities",new object[]{});
      return ((string[])(results[0]));
   }
}

[Visual Basic]

<WebServiceBinding(Name:="MyWebServiceSoap")> _
Public Partial Class MyWebService
      Inherits SoapHttpClientProtocol

   Public Sub New()
      ...
   End Sub
   
   <SoapDocumentMethod(...)> _
   Public Function GetCities() As String()
      Dim results As Object() = Invoke("GetCities", New Object(){})
      Return CType(results(0), String())
   End Function
End Class

[C++]

[WebServiceBinding(Name=L"MyWebServiceSoap")]
public ref class MyWebService : public SoapHttpClientProtocol 
{
   public: Service::Service() 
   {...}

   public:[SoapDocumentMethod(...)]
   cli::array<String^  >^ GetCities()
   {
      cli::array<Object^  >^ results = Invoke(L"GetCities",
                             gcnew cli::array<Object^>(0));
      return (cli::safe_cast<cli::array< System::String^>^>(results[0]);
    }   
}

 

About the author

Juval Lowy is a software architect and the principal of IDesign, specializing in .NET architecture consulting and advanced .NET training. Juval is Microsoft's Regional Director for the Silicon Valley, working with Microsoft on helping the industry adopt .NET. His latest book is Programming .NET Components 2nd Edition (O'Reilly, 2005). Juval participates in the Microsoft internal design reviews for future versions of .NET. Juval published numerous articles, regarding almost every aspect of .NET development, and is a frequent presenter at development conferences. Microsoft recognized Juval as a Software Legend as one of the world's top .NET experts and industry leaders.

Show: