List<string> as class property ?

  • Thread starter Thread starter GiJeet
  • Start date Start date
G

GiJeet

Hello, is it possible to have a List<whatever> as a property of a
class or must you use indexers? If so, please show me an example of
the syntax. Thanks.

G
 
GiJeet said:
Hello, is it possible to have a List<whatever> as a property of a
class or must you use indexers? If so, please show me an example of
the syntax. Thanks.

It is possible to have a property, or any number of properties, of type
List<whatever> in a class.


public class Foo
{
public List<string> List1 { ... }
public List<FileInfo> List2 { ... }
}

....

Foo foo = new Foo();
....
string firstString = foo.List1[0];
FileInfo firstFile = foo.List2[0];

This has nothing directly to do with creating indexers on Foo. But you
can use a field of a list type as the implementation of an indexer.
 
I was looking for properties using get/set. And how to assign the
list via the constructor?

Example:

//trying to create a list that's a property of a class holding IP
addresses of a computer
private List<string> lIPAddressList;
internal List<string> IPAddressList {
get { return lIPAddressList; }
set { lIPAddressList = value; }
}


//The constructor of the class
internal CustomException( ) {
this.sUserName = Environment.UserName;
this.sDomainName = Environment.UserDomainName;
this.sComputerName = Dns.GetHostName();

// what to assign the whole list to a property
//this.lIPAddress =
Dns.GetHostEntry(ComputerName).AddressList; //what to put here?

}

It's just a lit of strings. Could be an array or List<string> or
whatever, just need to assign to a collection.
 
GiJeet said:
I was looking for properties using get/set. And how to assign the
list via the constructor?

Example:

//trying to create a list that's a property of a class holding IP
addresses of a computer
private List<string> lIPAddressList;
internal List<string> IPAddressList {
get { return lIPAddressList; }
set { lIPAddressList = value; }
}


//The constructor of the class
internal CustomException( ) {
this.sUserName = Environment.UserName;
this.sDomainName = Environment.UserDomainName;
this.sComputerName = Dns.GetHostName();

// what to assign the whole list to a property
//this.lIPAddress =
Dns.GetHostEntry(ComputerName).AddressList; //what to put here?

}

It's just a lit of strings. Could be an array or List<string> or
whatever, just need to assign to a collection.
 
GiJeet said:
I was looking for properties using get/set. And how to assign the
list via the constructor?

Example:

//trying to create a list that's a property of a class holding IP
addresses of a computer
private List<string> lIPAddressList;
internal List<string> IPAddressList {
get { return lIPAddressList; }
set { lIPAddressList = value; }
}


//The constructor of the class
internal CustomException( ) {
this.sUserName = Environment.UserName;
this.sDomainName = Environment.UserDomainName;
this.sComputerName = Dns.GetHostName();

// what to assign the whole list to a property
//this.lIPAddress =
Dns.GetHostEntry(ComputerName).AddressList; //what to put here?

}


It's just a lit of strings. Could be an array or List<string> or
whatever, just need to assign to a collection.
 
I was looking for properties using get/set. And how to assign the
list via the constructor?

Example:

//trying to create a list that's a property of a class holding IP
addresses of a computer
private List<string> lIPAddressList;
internal List<string> IPAddressList {
get { return lIPAddressList; }
set { lIPAddressList = value; }
}


//The constructor of the class
internal CustomException( ) {
this.sUserName = Environment.UserName;
this.sDomainName = Environment.UserDomainName;
this.sComputerName = Dns.GetHostName();

// what to assign the whole list to a property
//this.lIPAddress =
Dns.GetHostEntry(ComputerName).AddressList; //what to put here?

}

It's just a lit of strings. Could be an array or List<string> or
whatever, just need to assign to a collection.

Assuming that the AddressList property above returns some enumerable
collection of strings you can use the List<> constructor that takes an
IEnumerable object:

IPAddressList = new
List<string>(Dns.GetHostEntry(ComputerName).AddressList);

(And yes, you can stick "this." onto the front of your property; that's just
not my preferred style.)
 
I was looking for properties using get/set. And how to assign the
list via the constructor?

Example:

//trying to create a list that's a property of a class holding IP
addresses of a computer
private List<string> lIPAddressList;
internal List<string> IPAddressList {
get { return lIPAddressList; }
set { lIPAddressList = value; }
}


//The constructor of the class
internal CustomException( ) {
this.sUserName = Environment.UserName;
this.sDomainName = Environment.UserDomainName;
this.sComputerName = Dns.GetHostName();

// what to assign the whole list to a property
//this.lIPAddress =
Dns.GetHostEntry(ComputerName).AddressList; //what to put here?

}

It's just a lit of strings. Could be an array or List<string> or
whatever, just need to assign to a collection.

As the others have said, assuming AddressList is compatible, then your
code would be fine. I want to point out two and a half things though...

1. Don't refer to lIPAddressList in your code. Use the property. At
some time later if your setter method does other things, such as raise
an event, you need not change anything then.
1.A. If you don't do anything special in the property, you can use the
property in this form:

internal List<string> IPAddressList {get; set;}

which helps prevent using the backing field directly.
2. You can always just initialize the list using: IPAddressList = new
List<string>.
 
Hello, is it possible to have a List<whatever> as a property of a
class or must you use indexers? If so, please show me an example of
the syntax.

As other have explained already then List<X> as type of
property is similar to all other types.

I will add that you should consider your object model
careful.

A property with both get and set of type List<X> is
not good encapsulation.

No set and a get that return a readonly List is fine.

..NET has the AsReadOnly method to return such a beast.

Arne
 
Back
Top