Home > Net >  Beginners have a problem, ask for help
Beginners have a problem, ask for help

Time:05-02

Directly on the program, the program is very simple, the problem is red font
Results only output stringTest, no output enumeratorTest, I do not know why, please people, I thank you very much!!!!!!!!!!

using System;
using System.Collections.Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;

Using System. The Collections;

The namespace ConsoleApplication2
{
Class Program
{
The static void Main (string [] args)
{
New test ();
Console.ReadLine();
}
}
The class test
{
Private IEnumerator enumeratorTest;
Private string stringTest;
The public test ()
{
enumeratorTest=enumeratorTest ();
StringTest=stringTest ();
}
Private IEnumerator EnumeratorTest ()
{
Console. WriteLine (" enumeratorTest ");
Yield return "1";
}
Private string StringTest ()
{
Console. WriteLine (" stringTest ");
Return "2";
}
}
}

CodePudding user response:

Wrote another small programs continue to test, the code is as follows:
Debugging found that returns the enumerator function does not perform in the call, but when using MoveNext iterative returns the enumerator will perform
The results shown in figure

using System;
using System.Collections.Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;

Using System. The Collections;

The namespace ConsoleApplication2
{
Class Program
{
The static void Main (string [] args)
{
The test t=new test ();
Console. WriteLine (" * * * * * * * * * * * * * * * * * * * * ");
Console. WriteLine (t.e numeratorTest. MoveNext ());
Console. WriteLine (t.e numeratorTest. Current);
Console. WriteLine (t.e numeratorTest. MoveNext ());
Console. WriteLine (t.e numeratorTest. Current);
Console. WriteLine (t.e numeratorTest. MoveNext ());
Console. WriteLine (t.e numeratorTest. Current);
Console. WriteLine (t.e numeratorTest. MoveNext ());

Console.ReadLine();
}
}
The class test
{
Public IEnumerator enumeratorTest;
Private string stringTest;
The public test ()
{
EnumeratorTest=enumeratorTest ();
StringTest=stringTest ();
}
Private IEnumerator EnumeratorTest ()
{
Console. WriteLine (" enumeratorTest - A ");
Yield return "A";
Console. WriteLine (" enumeratorTest - B ");
Yield return "B";
Console. WriteLine (" enumeratorTest - C ");
Yield return "C";
Console. WriteLine (" enumeratorTest - D ");
}
Public string StringTest ()
{
Console. WriteLine (" stringTest ");
return "0";
}
}
}
  •  Tags:  
  • C#
  • Related