I'm converting some code from VB.NET to C#. I have strange behavior
in an implemntation of an ArrayList. In VB.NET, the ArrayList is type
safe at compile time, where the C# is not type safe at compile time
(good). The C# code breaks at run time (bad). Here is the sample
code:
VB.NET
Public Class Country
Private _countryName As String
Private _capitalName As String
Public Sub New(ByVal countryName As String, ByVal capitalName As
String)
Me._countryName = countryName
Me._capitalName = capitalName
End Sub
Public ReadOnly Property CountryName() As String
Get
Return _countryName
End Get
End Property
Public ReadOnly Property CapitalName() As String
Get
Return _capitalName
End Get
End Property
End Class
Public Class CountryArrayLis t : Inherits ArrayList
Public Shadows Sub Add(ByVal inputCountry As Country)
MyBase.Add(inpu tCountry)
End Sub
End Class
Dim country1 As Country = New Country("USA", "Washington D.C.")
Dim country2 As Country = New Country("Canada ", "Ottawa")
Dim country3 As Country = New Country("France ", "Paris")
Dim country4 As Country = New Country("Austra lia", "Canberra")
Dim country5 As Country = New Country("Mexico ", "Mexico City")
Dim dr As DataRow = Nothing
Dim al As New CountryArrayLis t
al.Add(country1 )
al.Add(country2 )
al.Add(country3 )
al.Add(country4 )
al.Add(country5 )
al.Add(dr) ' compiler catches this :)
C# Code
public class Country
{
private string _countryName;
private string _capitalName;
public Country(string countryName, string capitalName)
{
this._countryNa me = countryName;
this._capitalNa me = capitalName;
}
public string CountryName
{
get {return _countryName;}
}
public string CapitalName
{
get {return _capitalName;}
}
}
public class CountryArrayLis t : System.Collecti ons.ArrayList
{
public new int Add(Country country)
{
return base.Add(countr y);
}
}
Country country1 = new Country("USA", "Washington D.C.");
Country country2 = new Country("Canada ", "Ottawa");
Country country3 = new Country("France ", "Paris");
Country country4 = new Country("Austra lia", "Canberra") ;
Country country5 = new Country("Mexico ", "Mexico City");
DataRow dr;
dr = null;
CountryArrayLis t al = new CountryArrayLis t();
al.Add(country1 );
al.Add(country2 );
al.Add(country3 );
al.Add(country4 );
al.Add(country5 );
al.Add(dr); // compiler does NOT catch this, runtime error :(
Any thoughts??
in an implemntation of an ArrayList. In VB.NET, the ArrayList is type
safe at compile time, where the C# is not type safe at compile time
(good). The C# code breaks at run time (bad). Here is the sample
code:
VB.NET
Public Class Country
Private _countryName As String
Private _capitalName As String
Public Sub New(ByVal countryName As String, ByVal capitalName As
String)
Me._countryName = countryName
Me._capitalName = capitalName
End Sub
Public ReadOnly Property CountryName() As String
Get
Return _countryName
End Get
End Property
Public ReadOnly Property CapitalName() As String
Get
Return _capitalName
End Get
End Property
End Class
Public Class CountryArrayLis t : Inherits ArrayList
Public Shadows Sub Add(ByVal inputCountry As Country)
MyBase.Add(inpu tCountry)
End Sub
End Class
Dim country1 As Country = New Country("USA", "Washington D.C.")
Dim country2 As Country = New Country("Canada ", "Ottawa")
Dim country3 As Country = New Country("France ", "Paris")
Dim country4 As Country = New Country("Austra lia", "Canberra")
Dim country5 As Country = New Country("Mexico ", "Mexico City")
Dim dr As DataRow = Nothing
Dim al As New CountryArrayLis t
al.Add(country1 )
al.Add(country2 )
al.Add(country3 )
al.Add(country4 )
al.Add(country5 )
al.Add(dr) ' compiler catches this :)
C# Code
public class Country
{
private string _countryName;
private string _capitalName;
public Country(string countryName, string capitalName)
{
this._countryNa me = countryName;
this._capitalNa me = capitalName;
}
public string CountryName
{
get {return _countryName;}
}
public string CapitalName
{
get {return _capitalName;}
}
}
public class CountryArrayLis t : System.Collecti ons.ArrayList
{
public new int Add(Country country)
{
return base.Add(countr y);
}
}
Country country1 = new Country("USA", "Washington D.C.");
Country country2 = new Country("Canada ", "Ottawa");
Country country3 = new Country("France ", "Paris");
Country country4 = new Country("Austra lia", "Canberra") ;
Country country5 = new Country("Mexico ", "Mexico City");
DataRow dr;
dr = null;
CountryArrayLis t al = new CountryArrayLis t();
al.Add(country1 );
al.Add(country2 );
al.Add(country3 );
al.Add(country4 );
al.Add(country5 );
al.Add(dr); // compiler does NOT catch this, runtime error :(
Any thoughts??
Comment