Hi,
I'm writing a simple LINQ to XML query. As often, the some elements might be missing for some nodes in the XML document. To solve this issue, I tried to use nullable types and the coalesce operator as proposed by Scott Guthrie. Whan effect that I noticed is that casting elements to (string?) dit not work while casting to (int?) worked just fine. An example:
var modules = from module in XMLConfig.Descendants("module")
select new MyApp.Modules.Manager
{
ManagerUrl = (string?)module.Element("Manager") ?? "http://localhost/default.asmx",
ManagerType = (int?) module.Element("ManagerType") ?? 1,
ManagerNumber = (int?) module.Element("ManagerNumber") ?? 1,
PrinterNr = (int?) module.Element("PrinterNr") ?? 1,
TextNr = (int?) module.Element("TextNr") ?? 100,
Name = module.Element("Name").Value
};
This gave me the compiler error:
Cannot convert type 'string?' to 'string'
However, there are no complaints when casting to (int?). If anyone could explain the reason for this behaviour (?) I would really appreciate it.