C # 7.0 - discard function value?

Looking through the new features C# 7.0, I got up with the drop function. It says:

Drops are local variables that you can assign but cannot read from. that is, they are write-only local variables.

and therefore follows an example:

if (bool.TryParse("TRUE", out bool _))

What is the real use case when it will be useful? I mean, if I defined it in the usual way, say:

if (bool.TryParse("TRUE", out bool isOK))
+4
source share
2 answers

discards - , . , , , , , , , :

public static void Main(string[] args)
{
    // I want to modify the records but I'm not interested
    // in knowing how many of them have been modified.
    ModifyRecords();
}

public static Int32 ModifyRecords()
{
    Int32 affectedRecords = 0;

    for (Int32 i = 0; i < s_Records.Count; ++i)
    {
        Record r = s_Records[i];

        if (String.IsNullOrWhiteSpace(r.Name))
        {
            r.Name = "Default Name";
            ++affectedRecords;
        }
    }

    return affectedRecords;
}

, ... , ( ), , .

, , , . String Boolean, , - . , String Boolean (a regular expression... if , ), , , , , .

, , :

public static void Main()
{
    var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
    Console.WriteLine($"Population change, 1960 to 2010: {pop2 - pop1:N0}");
}

private static (string, double, int, int, int, int) QueryCityDataForYears(string name, int year1, int year2)
{
    int population1 = 0, population2 = 0;
    double area = 0;

    if (name == "New York City")
    {
        area = 468.48;

        if (year1 == 1960) {
            population1 = 7781984;
        }

        if (year2 == 2010) {
            population2 = 8175133;
        }

        return (name, area, year1, population1, year2, population2);
    }

    return ("", 0, 0, 0, 0, 0);
}

, , , , discards , C#, .


Matlab discards , , , (, , ). ( ):

fileparts:

helpFile = which('help');
[helpPath,name,ext] = fileparts('C:\Path\data.txt');

: helpPath, name ext. . , . , .

, (~):

[~,name,ext] = fileparts(helpFile);

, Matlab , , , .

+7

:

TextBox.BackColor = int32.TryParse(TextBox.Text, out int32 _)? Color.LightGreen: Color.Pink;

, , . , , .

- , , - , , .

( Color.Yellow, -, , . , , "2 1". "2 1/2", , .)

+1

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


All Articles