Hello everyone,
New to C#, a simple question which my book does not cover.
If we do not specify the public/private access of get/set, then it is of the same as the public/private access to the property itself, but we can overwrite it.
For example, in the following code, in get method, when we do not specify public/private, it will be automatically the same as the property Abc, which makes get public, but in set, we can overwrite it to make it private?
[Code]
public class MyList
{
class Foo
{
private int _abc;
public int Abc
{
get
{
return _abc;
}
private set
{
_abc = value;
}
}
}
static void Main()
{
Foo f = new Foo();
return;
}
}
[/Code]
thanks in advance,
George
George GeorgePosted May 6, 2008, 8:56 AM
Great Brandon!
I read my question and your answer again.
Question:
--------------------
http://msdn.microsoft.com/en-us/library/75e8y5dd(VS.80).aspx
what makes me confused is -- "You cannot use accessor modifiers on an interface or an explicit interface member implementation",
does it mean we can not use accessor modifiers like public/private on interface property declaration or in class implementation?
--------------------
Your answer
--------------------
Correct =) Interface members are by default always declared public and you cant change them. That would defeat the purpose of having an interface,
--------------------
My question is XXX or YYY. Correct means? :-)
regards,
George
Posted May 4, 2008, 2:06 PM
As far as abstract classes go, you dont need to have abstract (inherit only) methods in it. They can follow standard method signatures :)
George GeorgePosted May 4, 2008, 1:25 AM
Cool, Brandon!
Great sample! It takes me sometime to read and learn. Two more comments,
1. "generic constraints" you mean where T : IPerson, right?
2. Abstract class has no need to must have abstract methods, right (like your class Person)?
regards,
George
Posted May 4, 2008, 12:41 AM
public interface IPerson
{
char Gender
{
get;
}
string Name
{
get;
set;
}
string PhoneNumber
{
get;
set;
}
}
public class Job
{
private T person;
public Job(T person)
{
this.person = person;
}
public void GetWorkDone()
{
//No discrimination meant :)
if (person.Gender == 'M')
{
MessageBox.Show("Im male and Im doing some work. My name is " + person.Name + " glad to meet you!", "Hello!");
}
else if (person.Gender == 'F')
{
MessageBox.Show("Hello! Im female and Im doing some work. My phone number is " + person.PhoneNumber + " glad to meet you!", "Hello!");
}
else
{
throw new ArgumentException("This person should definitely see a doctor!");
}
}
}
Basically, when declaring an interface and implementing it on a class, you can say "This class is guaranteed to have these member" which works good for forcing generic constraints. Then, when you create a class with a generic type parameter, you can create the class definition with members that are guaranteed by that constraint. A generic constraint means a few things, two of which are:
1) Any types you attempt to set as the type parameter of this class MUST implement the IPerson interface.
2) Since only classes that implement the IPerson interface can be used as generic type parameters, then the members are guaranteed and therefore you can write the class body using members from the IPerson interface.
If you want to store members or initialize base members, use an abstract class. It works much like an interface, but you can inherit from it and have preset values and method/property bodies. Like so:
public abstract class Person
{
protected char gender = 'M';
protected string name = "John Doe";
protected string phoneNumber = "000-000-0000";
public virtual char Gender
{
get { return this.gender; }
}
public virtual string Name
{
get { return this.name; }
set { this.name = value; }
}
internal virtual string PhoneNumber
{
get { return this.phoneNumber; }
set { this.phoneNumber = value; }
}
}
An abstract class is only inheritable, you cannot create instances of it. Just as sealed classes cannot be inherited, only used to create objects. It just really depends on what you wanna do =D If you have classes like these:
public class Me : IPerson
{
private char gender = 'M';
private string name = "ScrapIron";
private string phoneNumber = "123-456-7890";
public char Gender
{
get { return this.gender; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string PhoneNumber
{
get { return this.phoneNumber; }
set { this.phoneNumber = value; }
}
}
public class You : IPerson
{
private char gender = 'M';
private string name = "George George";
private string phoneNumber = "111-222-3333";
public char Gender
{
get { return this.gender; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string PhoneNumber
{
get { return this.phoneNumber; }
set { this.phoneNumber = value; }
}
}
Then... create instance of jobs:
//Create new jobs using us as workers.
Job<Me> myJob = new Job<Me>(new Me());
Job<You> yourJob = new Job<You>(new You());
//Lets do some work.
myJob.GetWorkDone();
yourJob.GetWorkDone();
And you know what else is cool? If you dont know what kind of object is going to be created until runtime, you can do something like this:
Job<IPerson> unknownPersonsJob = new Job<IPerson>((IPerson)new Me());
or
Kind of a poor example but youll see where Im going when you get more used to this sort of thing. It doesnt matter because both classes implement the IPerson interface. This also works with base classes such as inheriting from an abstract or superclass. Lots to absorb, but itll all become second nature the more you use it =D
George GeorgePosted May 4, 2008, 12:00 AM
Cool!
I have found some more materials from,
http://msdn.microsoft.com/en-us/library/75e8y5dd(VS.80).aspx
what makes me confused is -- "You cannot use accessor modifiers on an interface or an explicit interface member implementation",
does it mean we can not use accessor modifiers like public/private on interface property declaration or in class implementation?
regards,
George
Posted May 3, 2008, 7:39 PM