How are C # 6.0 default property values ​​assigned when using function calls?

I might get some downvotes here, but I actually found conflicting information in a regular search and would like to get a definitive answer that other people can also easily find.

Given a property in current C #:

public static IEnumerable<string> foo { get; set; } = new string[] { "bar", "bar2" };

We know that the default value foowill be returned as the above array. If a different value is assigned, the default value is no longer used. However, if so:

public static IEnumerable<string> foo { get; set; } = GetMyStrings();

My thought is that these are functions:

if(foo == null) { foo = GetMyStrings();}

and that foo will save the value from the first run GetMyStrings()for the lifetime of the object, unless overwritten by the manually assigned value.

, GC GetMyStrings() , "" GetMyStrings() .

?

+4
3

, :

static ClassName()
{
    foo = GetMyStrings();
}

( , ).

, GC, GetMyStrings() , "" GetMyStrings() .

. , , .

+5

public static IEnumerable<string> Foo { get; set; } = GetMyStrings();

public static IEnumerable<string> Foo { get; set; }

, :

static MyClass() {
    Foo = GetMyStrings();
}

( , Foo = GetMyStrings(); ).

, : , GetMyStrings() , it return Foo. , , .

"" , . GetMyStrings(), , , , # 6.

, Foo , , :

public static IEnumerable<string> Foo { get; } = GetMyStrings();
0

, :

public void RunTest()
{
    Test t = new Stackoverflow.Form1.Test();
    Console.WriteLine(t.Values.First());
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine(t.Values.First());
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine(t.Values.First());

    Console.WriteLine("------");
    Console.WriteLine(t.Values2.First());
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine(t.Values2.First());
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine(t.Values2.First());

    Console.WriteLine("------");
    Console.WriteLine(t.Values3.First());
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine(t.Values3.First());
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine(t.Values3.First());
}

public class Test
{
    public IEnumerable<string> Values { get; set; } = GetValues();

    public static IEnumerable<string> GetValues()
    {
        List<string> results = new List<string>();
        for (int i = 0; i < 10; ++i)
        {
            yield return DateTime.UtcNow.AddMinutes(i).ToString();
        }
    }

    public IEnumerable<string> Values2 { get; set; } = GetValues2();

    public static IEnumerable<string> GetValues2()
    {
        return GetValues().ToList();
    }

    public IEnumerable<string> Values3 { get; set; } = GetValues().ToList();
}

:

19/04/2017 12:24:25
19/04/2017 12:24:26
19/04/2017 12:24:27
------
19/04/2017 12:24:25
19/04/2017 12:24:25
19/04/2017 12:24:25
------
19/04/2017 12:24:25
19/04/2017 12:24:25
19/04/2017 12:24:25

: , IEnumerable < > yield , .

2: . List IEmnumerable, List ( IEnumerable), .

3: .ToList() GetValues2() . , 2, .

, IEnumerable, , .

, Linq - . , , Linq.


Ask a garbage collection question. Objects referenced by the active object will not collect garbage.

0
source

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


All Articles