If I have VB declaration like this Public ReadOnly Property Document() As XmlDocument
, what is its C# equivalent? Thanks.
views:
215answers:
6
+7
A:
public XmlDocument Document { get; private set; }
Edit as per comments... Thanks guys, didn't even try to see if it would compile.
thorkia
2009-12-22 20:39:19
heh... beat me by one second. :D
Randolpho
2009-12-22 20:40:10
Won't compile...
PostMan
2009-12-22 20:41:43
C# doesn't support "readonly" declarations like that.
Mehrdad Afshari
2009-12-22 20:42:17
@PostMan, Mehrdad, Nescio - fixed, thanks for the comments
thorkia
2009-12-22 20:46:12
A:
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
}
David Basarab
2009-12-22 20:39:20
+9
A:
You can use automatic properties in C# 3.0+ to achieve the same thing:
public XmlDocument Document { get; private set; }
Mehrdad Afshari
2009-12-22 20:39:59
This is slightly different though: this allows Document to be modified through the property, which the VB version doesn't. (The OP doesn't show the implementation of the VB property, but e.g. this might be a calculated property, or back onto a readonly field.)
itowlson
2009-12-22 20:45:30
itowlson: Yes, sure. There's no direct simple translation. You have to manually implement the property in that case.
Mehrdad Afshari
2009-12-22 20:46:13
+1
A:
VB.Net requires you to write read-only, but C# you only need to exclude the setter part of the property.
Wade73
2009-12-22 20:40:26
+6
A:
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/
martani_net
2009-12-22 20:42:31
Rats. I saw this softball lobbed in too late to post a link to this site. +1
Mark Wilkins
2009-12-22 20:44:28