简体   繁体   中英

c# how to code a class method that can be called on the instance of the class

using c# .net 4.7.1 I have a class called Deck , here's the code:

public class Deck
{
    public Card[] DeckOfCards { get; private set; } = new Card[56];        

    public Deck()
    {
        DeckOfCards = NewDeck();
    }

    private Card[] NewDeck()
    {
        Card[] newDeck = new Card[56];
        ....
        return newDeck;
    }
}

I would like to declare a class method within Deck called Shuffle that I would be able to call on the instance of Deck that I create. Here's how I picture my Main method in my program looking like:

class program
{
    static void Main(string[] args)
    {
        Deck WorkingDeck = new Deck();
        WorkingDeck.DeckOfCards.Shuffle();
        ....
    }
}

Here's all I have for the class method Shuffle so far:

    public Card[] Shuffle(this Card[] DeckToShuffle)
    {
        Random rnd = new Random();
        Card[] ShuffledDeck = DeckToShuffle.OrderBy(x => rnd.Next()).ToArray();
        return ShuffledDeck;
    }

But this requires that I pass a Card[] in my method call. I would like the method to use WorkingDeck.DeckOfCards as the Deck that gets shuffled. Can someone point me in the right direction to be able to do this? Thanks in advance.

In this particular scenario, an Extension method would be a good solution.

public static class Extensions
{
    public static void Shuffle(this IEnumerable<Card> source)
    {
        // Body of Shuffle
    }
}

This would help you call shuffle as you desired

Deck WorkingDeck = new Deck();
WorkingDeck.DeckOfCards.Shuffle();

First, you can simplify your Deck class as follows:

public class Deck {

    public Card[] DeckOfCards { get; private set; } = NewDeck();

    private static Card[] NewDeck() {
        Card[] newDeck = new Card[56];
        ....
        return newDeck;
    }
}

If you wish to make Shuffle change the order of cards in your Deck , you could write an extension method, like this:

static class DeckExtensions {

    public static void Shuffle(this deck)
    {
        Random rnd = new Random();
        deck.DeckOfCards = deck.DeckOfCards.OrderBy(x => rnd.Next()).ToArray();
    }

}

The DeckOfCards property is a member of the class. A method on a class can automatically access its members (either implicitly or via the this keyword).

public class Deck
{
    public Card[] DeckOfCards { get; private set; };        

    public Deck()
    {
        this.DeckOfCards = NewDeck();
    }

    static Card[] NewDeck()
    {
        Card[] newDeck = new Card[56];
        ....
        return newDeck;
    }

    public void Shuffle()
    {
        Random rnd = new Random();
        Card[] shuffledDeck = this.DeckOfCards.OrderBy(x => rnd.Next()).ToArray();
        this.DeckOfCards = shuffledDeck;
    }
}

You would invoke it like this:

static void Main(string[] args)
{
    Deck workingDeck = new Deck();
    workingDeck.Shuffle();
    ....
}

This design has Deck as a mutable object: Shuffle alters the order of the existing deck, rather than creates a different deck.

As an aside, there are better shuffling methods to use. The term to Google here is 'Fisher-Yates algorithm'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM