Rockitdev
Rockitdev

Reputation: 155

What is it called when a class property is an instance of another class?

Very basic question here, look at my property Order in my customer class. Wondering what is the formal name of a property type like this is (yes, this could also be a list).

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }

    public Order Orders { get; set; }  // what am i called?
}

public class Order
{
    public int ID { get; set; }
    public string SomeProperty { get; set; }
}

Upvotes: 0

Views: 254

Answers (5)

Patrick M
Patrick M

Reputation: 93

The concept itself is called Composition. Basically, you want to be able to use a Customer object to get information about an Order, but you don't want the logic that gets that information to live in Customer. So, you have a member who is an Order and Order encapsulates the Order behavior.

You could say that a Customer is composed of Order along with other values.

Have a link: http://www.javaworld.com/jw-11-1998/jw-11-techniques.html

Not that you asked this, but you probably will want an actual collection of Orders. You could start with
public List<Order> Orders;

Upvotes: 1

Habib
Habib

Reputation: 223322

Its the same thing. Its called a "Property". There is no different name for it. Consider your SomeProperty which is of type string. string is also a class and SomeProperty is its object. Same convention with your class would follow as well.

From C# Language Specification.

1.6.7.2 Properties

A property is declared like a field, except that the declaration ends with a get accessor and/or a set accessor written between the delimiters { and } instead of ending in a semicolon.

So the term "property" in C# is associated with the accessors (get/set)

Upvotes: 4

jltrem
jltrem

Reputation: 12524

from ECMA-334 8.7.4:

A property is a member that provides access to a characteristic of an object or a class.

It doesn't matter what type the property accesses. The property itself is just to provide access to it.

So, bottom line, a property is a property no matter what type it accesses.

Upvotes: 2

James Johnson
James Johnson

Reputation: 46067

It's still a property. It just gets/sets an object, which is an instance of a class.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152626

It's just a property - there's not a formal name for it.

Upvotes: 1

Related Questions