public class SecretNumber
{
//Construktor
public SecretNumber()
{
Initialize();
_previousGuesses = new List<int>();
}
//Constant
public const int MaxNumberOfGuesses = 7;
//Field
private int _number;
private List<int> _previousGuesses;
//Property
public bool CanMakeGuess
{
get;
}
public int Count
{
get;
}
public int? Number
{
public get;
}
//Lite oklart hur man ska göra när fälten är autoimplementerade
public Outcome Outcome1
{
get
{
return Outcome1;
}
private set
{
Outcome1 = value;
}
}
public ReadOnlyCollection<int> PreviousGuesses
{
get;
set;
}
//Methods
public void Initialize()
{
Random random = new Random();
_number = random.Next(1, 100);
_previousGuesses.Clear();
Outcome1 = Outcome.Indefinite;
}
private Outcome MakeGuess(int guess)
{
if (Number > 1 && Number < 100)
{
if (PreviousGuesses.Contains(guess))
{
return Outcome.PreviousGuess;
}
else if (PreviousGuesses.Count >= MaxNumberOfGuesses)
{
return Outcome.NoMoreGuesses;
}
if (Number == guess)
{
return Outcome.Correct;
}
else if (Number < guess)
{
return Outcome.High;
}
// else if (Number > guess)
// {
return Outcome.Low;
// }
}
else
{
throw new ArgumentOutOfRangeException("Måste vara inom intervallet 1-100");
}
}
//Enumerator
enum Outcome
{
Indefinite,
Low,
High,
Correct,
NoMoreGuesses,
PreviousGuess
}
}
我收到错误消息,我的枚举Secret Number的可访问性比属性Outcome1少,但我不知道为什么。
该属性称为Outcome1,因为否则名称在Visual Studio中混合使用。
最佳答案
您的枚举具有private
可见性,因为您没有给它明确的可见性,并且它嵌套在您的SecretNumber
类中。
当该类型也未公开时,您不能使用该类型作为参数或返回公开公开的成员的类型-否则,调用者将不知道如何处理它。
您可以将其公开:
public enum Outcome
1与“公共成员”并不完全相同。例如,
public
类型的internal
(非覆盖)方法可以将其他内部成员称为参数-因为该方法仍只能由知道“包含” internal
类型的另一种类型调用。无论如何。关于c# - 枚举的可访问性少于属性结果1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9435184/