[英]How do you create a method in C# that takes a multiple set amount of values?
I'm wondering how in C# I would go about creating a method that would take a couple set values, kind of like an enum. 我想知道如何在C#中创建一个需要几个设定值的方法,有点像枚举。
public static void SwitchState(enum State { On, Off, Idle }) {
}
Something like that I don't know...I don't want to have strings passed into the method because then I have to memorize what those strings are. 我不知道这样的事情...我不想让字符串传递到方法中,因为那样我就必须记住那些字符串是什么。
public enum State { On, Off, Idle }
public static void SwitchState(State state) {
// ...
}
You actually can use enums like this 您实际上可以使用这样的枚举
public enum SpotifyAction
{
PlayPause,
Next,
Previous,
VolumeUp,
VolumeDown,
Mute,
Quit
};
public static void controlSpotify(SpotifyAction sa)
{
BringToForeground("Spotify");
switch (sa)
{
case SpotifyAction.Next:
SendKeys.SendWait("^({RIGHT})");
break;
case SpotifyAction.Previous:
SendKeys.SendWait("^({LEFT})");
break;
case SpotifyAction.VolumeUp:
SendKeys.SendWait("^({UP})");
break;
case SpotifyAction.VolumeDown:
SendKeys.SendWait("^({DOWN})");
break;
case SpotifyAction.PlayPause:
SendKeys.SendWait(" ");
break;
}
}
You just create an enum and then pass it to the method. 您只需创建一个枚举,然后将其传递给该方法。
public enum State
{
On,
Off,
Idle
}
private void SwitchState(State stateValue)
{
}
private void testCall()
{
SwitchState(State.Idle);
}
void Main()
{
SwitchState(State.On | State.Off);
}
public void SwitchState(State s)
{
//...
}
public enum State
{
None = 0,
On = 1,
Off = 2,
Idle = 4
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.