I have this piece of code running in .net 3.5
public const string SvgNamespace = "http://www.w3.org/2000/svg";
public const string XLinkPrefix = "xlink";
public const string XLinkNamespace = "http://www.w3.org/1999/xlink";
public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
public static readonly List<KeyValuePair<string, string>> Namespaces = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("", SvgNamespace),
new KeyValuePair<string, string>(XLinkPrefix, XLinkNamespace),
new KeyValuePair<string, string>("xml", XmlNamespace)
};
private bool _inAttrDictionary;
private string _name;
private string _namespace;
public string NamespaceAndName
{
get
{
if (_namespace == SvgNamespace)
return _name;
return Namespaces.First(x => x.Value == _namespace).Key + ":" + _name;
}
}
and I am currently converting it to .net 2.0 (removing System.Linq). How can I maintain the functionality of Enumerable.First Method (IEnumerable, Func) found here within my code?
Full source file
You can probably use a foreach
loop like
foreach(var item in Namespaces)
{
if(item.Value == _namespace)
return item.Key + ":" + _name;
}
You can create GetFirst method as follows:
public string NamespaceAndName
{
get
{
if (_namespace == SvgNamespace)
return _name;
return GetFirst(Namespaces, _namespace).Key + ":" + _name;
}
}
private KeyValuePair<string, string> GetFirst(List<KeyValuePair<string,string>> namespaces,string yourNamespaceToMatch)
{
for (int i = 0; i < namespaces.Count; i++)
{
if (namespaces[i].Value == yourNamespaceToMatch)
return namespaces[i];
}
throw new InvalidOperationException("Sequence contains no matching element");
}
It's not really an alternative to Enumerable.First
, but since you have actually a List<T>
variable, you can consider Find
method. The signature is compatible with the Enumerable.First
, but note that the behavior is compatible with Enumerable.FirstOrDefault
, ie in case the element doesn't exist, you'll get NRE instead of the "Sequence contains no matching element".
return Namespaces.Find(x => x.Value == _namespace).Key + ":" + _name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.