What's the equivalent of VB ReadOnly property in C#?
If I have VB declaration like this Public ReadOnly Property Document() As XmlDocument
, what is its C# equiv开发者_如何学Goalent? Thanks.
public XmlDocument Document
{
get {return someXmlDoc;}
}
You can use automatic properties in C# 3.0+ to achieve the same thing:
public XmlDocument Document { get; private set; }
public XmlDocument Document { get; private set; }
Edit as per comments... Thanks guys, didn't even try to see if it would compile.
Here is a great tool that convert automatically VB.NET code to C# and vise versa http://www.developerfusion.com/tools/convert/vb-to-csharp/
VB.Net requires you to write read-only, but C# you only need to exclude the setter part of the property.
public XmlDocument Document { get; private set; } // For .NET 3.5
For Previous Versions
private XmlDocument _document;
public readonly XmlDocument Document
{
get
{
return _document;
}
// You don't need a setter
}
精彩评论