Using the whole class as a parameter is good practice?

Suppose a class called "Location" stores locations and related data:

class Place
{
    string Name;
    string Location;
    // etc...
}

Later we fill out the name and details to 80 different cities. Therefore, we store these cities numerically in an array:

class Setup
{
    public static GetTowns
    {
        for (int i = 1; i <= numberOfTowns; i++)
        {
            Town[i] = new Place(name, location);
        }
        // more code...
    }
    // more methods...
}

To access a specific city and its data, we pass the city itself as a parameter and get it:

public static void DescribeTown(Place Town)
{
    // example method from some other class
    Console.WriteLine("Shipping to {0}.", Town.Name);
}

Other methods require access to several cities or all of them. Then we can pass the entire Town array as a parameter:

public static void ListAllTowns(Place[] Town)
{
    Console.WriteLine("Our Beloved Clients:");
    foreach (Place here in Town)
    {
        Console.WriteLine("{0} in {1}", here.Name, here.Location);
        // Various other operations requiring info about the Towns
    }
}

The full C # 2.0 reference states the following

Parameters are variables that receive the value of the arguments passed to the method when it is called.

, ListAllTowns , . , ?

( , .)

+5
6

# , .

: . (.. - ). , , , . .

, : structs ( ). , .

+2

. , , ListAllTowns ( ).

( ) (-).

+7

( ref out), , .

, , .

+4

.NET . , .

, ( , , Towns, Towns )

+1

.

- Occam Razor , - , onther - DescribeTown(Place Town) DescribeTown(string TownName) , OOP-, , , , .

, , , Town , DescribeTown(string TownName).

- . DescribeTown(string TownName), DescribeTown(Place Town), , , , ( , ).

+1

, . , Robert Martins Clean Code. , , . , Town ListAllTowns "ListAll" Place, :

Towns.ListAll()

In this sense, you will not need to pass a list of functions every time it is used, because it will just work on it that owns the object.

0
source

All Articles