[Mono-dev] private members

Vladimir Giszpenc vgiszpenc at dsci.com
Mon Dec 10 09:19:08 EST 2007


Hi,

Given the following program, I have a question about Mono and .Net.  This is
not a bug in Mono because I think the same thing happens on Windows, but I
was hoping someone could explain it to me.  The line below with the comment
is surprising because it is accessing a private variable of another
instance.  Why does it compile?  Shouldn't it say that member bar is not
visible?

Thanks,

Vlad



using System;

namespace Test
{
	public class Foo
	{
		private string bar = null;
		
		public Foo(string bar)
		{
			this.bar = bar;
		}
		private Foo(Foo foo)
		{
			this.bar = foo.bar; //This line is surprising
		}
		
		public Foo Copy()
		{
			return new Foo(this);
		}
	}
	class MainClass
	{
		public static void Main(string[] args)
		{
			Foo foo = new Foo("foobar");
			Foo foobar = foo.Copy();
		}
	}
}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3329 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071210/bcff53c9/attachment.bin 


More information about the Mono-devel-list mailing list