The problem of converting an array of structures from C ++ to C #

I want to convert this C ++ code to C #:

typedef struct consoleCommand_s 
{
    char* cmd          ;
    void (*function)() ;
} consoleCommand_t     ;



static consoleCommand_t commands[] = 
{
    {"clientlist", &CG__Clientlist},
    {"say", &CG_Say},
    {"nick", &CG_Nick},
    {"logout", &CG_Logout}
} ;

// e.g.
static void CG_Logout(void)
{
    // Logout
}

The closest I came is:

 public class Class1
    {
        // public delegate int Calculate (int value1, int value2);
        public delegate void fnCommand_t();

        public class consoleCommand_t
        {
            public string strCommandName; 
            public fnCommand_t fnCommand;

            public consoleCommand_t(string x, fnCommand_t y)
            {
                this.strCommandName = x;
                this.fnCommand = y;
            } // End Constructor

        } // End Class consoleCommand_t


        public static void Nick()
        {
            Console.WriteLine("Changing Nick");
        } // End Sub


        public static void Logout()
        {
            Console.WriteLine("Logging out");
        } // End Sub

        // string[] names = new string[] {"Matt", "Joanne", "Robert"};
        public consoleCommand_t[] commands = new consoleCommand_t[] {
            new consoleCommand_t("Nick",   Nick),
            new consoleCommand_t("Logout", Logout)
        };





    } // End Class Class1

Now I wanted to ask:

A) Why should Nick and Logout be static if everything else is not?
B) Is there a C-like way to initialize an array of commands, that is, without new ones?

+3
source share
3 answers

You can dispense with the INSIDE class of another class and manage the default console commands in another.

public delegate void ConsoleCmd();

public class DefaultCommands
{
    public static void Nick()
    {
        Console.WriteLine("I'm Nick!");
    }

    public static void LogOut()
    {
        Console.WriteLine("You're Fired!");
    }
}

public class Console
{
    private Dictionary<string, ConsoleCmd> mCommands;

    public Console()
    {
        mCommands = new Dictionary<string, ConsoleCmd>();
        mCommands.Add("Nick", DefaultCommands.Nick);
        mCommands.Add("Logout", DefaultCommands.LogOut);
    }
}

Access to your teams will be as easy as:

ConsoleCmd command = mCommands["Nick"];
command();

. . OP, , . , , , , - .

+3

:

    public void Nick(){}
    public void Logout(){}

   public consoleCommand_t[] commands = new consoleCommand_t[] {
        new consoleCommand_t("Nick",   this.Nick), 
        new consoleCommand_t("Logout", this.Logout) 
   };
+3

B) C- , ?

Don't dwell on newthe C # keyword , this is exactly how the initialization is written. In C #, a similar distinction between stack allocation and heap allocation is made using struct vs class. (This way you can create your ConsoleCommand_t structure.)

Edit: And using the structure, you can also:

new consoleCommand_t() {strCommandName = "Nick", fnCommand = Nick}

And skip the consoleCommand_t constructor entry. This is about the same as you can get to initializing a C-style structure, I think

+1
source

Source: https://habr.com/ru/post/1793616/


All Articles