Thursday, April 03, 2008

C# quiz: 6/?

Consider the following:

    class Test {
private int m_foo;
public int Foo {
get { return m_foo; }
}
public int set_Foo(int value) {
m_foo = value;
}
}

What happens when you compile, and why?


2 comments:

Roman V. Gavrilov said...

public int set_Foo(int value) {
m_foo = value;
}

Was method meant to be void?

Vidhya said...

It gives the following compilation error.
Type 'WpfAppln1.Test' already reserves a member called 'set_Foo' with the same parameter types

When we define a property in c#, get_PropName and set_PropName methods gets allocated by the compiler for get and set(that we usually define within the property), so that the class should not have same method names irrespective of the return type. I think my understanding is correct, if not let me know.