c# using Array.ForEach Action Predicate with array of value type or string

899

Solution 1

You are simply changing the local variable - not the item in the list. To do this, you want ConvertAll, which creates a new list/array:

    int[] ints = new int[] { 1,2 };

    int[] newArr = Array.ConvertAll<int,int>(ints, AddTen);

with:

static int AddTen(int i)
{
    return i+10;
}

This can also be written with an anonymous method:

int[] newArr = Array.ConvertAll<int,int>(ints,
    delegate (int i) { return i+ 10;});

Or in C# 3.0 as a lambda:

int[] newArr = Array.ConvertAll(ints, i=>i+10);

Other than that... a for loop:

for(int i = 0 ; i < arr.Length ; i++) {
    arr[i] = AddTen(arr[i]); // or more directly...
}

But one way or another, you are going to have to get a value out of your method. Unless you change the signature, you can't.

Solution 2

The Array.ForEach() method won't let you practically modify it's members. You can modify it by using LINQ such as this

ints = ints.Select(x => x +10 ).ToArray();
Share:
899
connor
Author by

connor

Updated on June 25, 2022

Comments

  • connor
    connor almost 2 years

    Had issues last night with MySQL. I uninstalled it and then reinstalled it and now I cannot get the server to connect after following some uninstall instructions here:

    https://community.jaspersoft.com/wiki/uninstall-mysql-mac-os-x

    ^ I think this tutorial above may be the cause of many of my socket issues that are happening now

    log file:

    2019-05-13T17:35:42.702359Z 0 [System] [MY-010910] [Server] /usr/local/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.16)  MySQL Community Server - GPL.
    2019-05-13T17:35:51.255099Z 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.16) starting as process 78228
    2019-05-13T17:35:51.258608Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
    2019-05-13T17:35:51.635376Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    2019-05-13T17:35:51.641789Z 0 [ERROR] [MY-010270] [Server] Can't start server : Bind on unix socket: Address already in use
    2019-05-13T17:35:51.641800Z 0 [ERROR] [MY-010258] [Server] Do you already have another mysqld server running on socket: /tmp/mysql.sock ?
    

    netstat outlook:

    ➜  invisible-hand git:(2.1.0) ✗ netstat -na | grep LISTEN
    tcp4       0      0  127.0.0.1.53           *.*                    LISTEN     
    tcp4       0      0  *.52772                *.*                    LISTEN     
    tcp4       0      0  *.52607                *.*                    LISTEN     
    tcp4       0      0  127.0.0.1.4244         *.*                    LISTEN     
    tcp4       0      0  *.57621                *.*                    LISTEN     
    tcp4       0      0  127.0.0.1.63777        *.*                    LISTEN     
    tcp4       0      0  127.0.0.1.62722        *.*                    LISTEN     
    tcp6       0      0  *.61500                *.*                    LISTEN     
    tcp4       0      0  *.61500                *.*                    LISTEN     
    tcp4       0      0  127.0.0.1.1023         *.*                    LISTEN     
    tcp4       0      0  127.0.0.1.29754        *.*                    LISTEN     
    tcp4       0      0  127.0.0.1.29834        *.*                    LISTEN
    

    After editing the my.cnf file to change the port in use it still has the output below:

    $ mysql -uroot -p
    Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (61)
    
    $ mysql_secure_installation
    Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (61)
    
    /usr/local/etc/my.cnf
    # Default Homebrew MySQL server config
    [mysqld]
    # Only allow connections from localhost
    bind-address = 127.0.0.1
    
    [client]
    port = 3306
    socket = /tmp/mysql.sock
    

    Inspecting the settings->MySQL the server turns on and off again.

    MySQL settings/instance GUI: MySQL instance shuts off after 2-4 seconds every time after start the instance, have tried uninstalling and reinstalling multiple times and the issue still persists.

    I have tried installing through brew and still no luck.

    Here is an output of my processes:

    ➜  invisible-hand git:(2.1.0) ✗ ps aux | grep mysql   
    user         74665   0.0  0.0  4277256    808 s000  S+   10:13AM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mysql
    
    ➜  invisible-hand git:(2.1.0) ✗ ps aux | grep mysqld
    user         74676   0.0  0.0  4277256    808 s000  S+   10:13AM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mysqld
    

    How can I fix the socket issue and get the instance running reliably without constantly crashing? Thank you!

  • JaredPar
    JaredPar about 15 years
    @Marc, learn something new every day. Hadn't seen that method before. Thanks!