开发者

What's the equivalent of VB ReadOnly property in C#?

开发者 https://www.devze.com 2022-12-14 12:48 出处:网络
If I have VB declaration like this Public ReadOnly Property Document() As XmlDocument, what is its C# equiv开发者_如何学Goalent? Thanks.public XmlDocument Document

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消