e.PaintParts &= ~DataGridViewPaintParts.Focus;
個人筆記內有紀錄過 Enum Attribute 兩種移除方式// 寫法一
var result = EnumSource ^ FlagRemove;
// 寫法二
var result = EnumSource & ~FlagRemove;
本篇筆記重點在於了解 EnumSource & ~FlagRemove; 和 EnumSource &= ~FlagRemove; 兩者差異
- EnumSource & ~FlagRemove:不會移除 EnumSource 內的 FlagRemoveEnum,但回傳值是移除的
- EnumSource &= ~FlagRemove:直接移除 EnumSource 內的 FlagRemoveEnum
寫個簡易範例來驗證
using System;
namespace EnumFlags
{
class Program
{
static void Main(string[] args)
{
Days source = Days.Tuesday | Days.Thursday | Days.Saturday;
Days flagRemove = Days.Thursday;
Console.WriteLine($"初始值:{source}");
// 使用 source & ~flagRemove 不會改變 source
Days result = source & ~flagRemove;
Console.WriteLine($"source & ~flagRemove result 值:{result} "); // Output: Tuesday、Saturday
Console.WriteLine($"source & ~flagRemove source 值:{source} "); // Output: Tuesday、Thursday、Saturday
// 使用 source &= ~flagToRemove 直接改變 source
source &= ~flagRemove;
Console.WriteLine($"source &= ~flagToRemove source 值:{source}"); // Output: Tuesday、Saturday
}
}
// 沒有特別宣告的話,Enum 預設是 int
[Flags]
public enum Days
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64,
All = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
};
}
沒有留言:
張貼留言