第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將標(biāo)準(zhǔn) DayOfWeek 與 Flag 進(jìn)行比較

如何將標(biāo)準(zhǔn) DayOfWeek 與 Flag 進(jìn)行比較

C#
撒科打諢 2023-04-29 09:57:01
我有自己的 DaysOfWeek Flag 枚舉(類似于https://learn.microsoft.com/en-us/previous-versions/ms886541(v=msdn.10))[Flags]public enum DayOfWeek{    Sunday = 0,    Monday = 1,    Tuesday = 2,    Wednesday = 4,    Thursday = 8,    Friday = 16,    Saturday = 32}我需要將標(biāo)準(zhǔn)的 DayOfWeek 與我的進(jìn)行比較。我怎樣才能做到這一點(diǎn)?
查看完整描述

4 回答

?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

由于您的枚舉使用與內(nèi)置相同的天數(shù)順序DayOfWeek,因此您需要做的就是使用DayOfWeektype 的變量作為的指數(shù)2,然后將其與您的枚舉變量按位與運(yùn)算。


像這樣的東西(這將檢查Monday你的枚舉標(biāo)志1是否存在):


MyDayOfWeek Days = MyDayOfWeek.Monday | MyDayOfWeek.Friday;

DayOfWeek D = DayOfWeek.Monday;

var Mask = 1 << (int)D;


if ((Mask & (int)Days) == Mask)

? //Do whatever;

我已將您的枚舉重命名為MyDaysOfWeek,而DayOfWeek是內(nèi)置 .NET 類型。你可以在一周中的任何一天做同樣的事情。

查看完整回答
反對(duì) 回復(fù) 2023-04-29
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

最后,我有解決方案


擁有帶有標(biāo)志的 DaysOfWeek:


[Flags]

public enum DaysOfWeek

{

? ? None = 0,

? ? Sunday = 1 << 0,

? ? Monday = 1 << 1,

? ? Tuesday = 1 << 2,

? ? Wednesday = 1 << 3,

? ? Thursday = 1 << 4,

? ? Friday = 1 << 5,

? ? Saturday = 1 << 6,

}

由于自己的枚舉具有相同的天數(shù),我們可以編寫擴(kuò)展方法將標(biāo)準(zhǔn) DayOfWeek 轉(zhuǎn)換為自己的


public static class EnumExtensions

{

? ? public static DaysOfWeek ToFlag(this DayOfWeek dayOfWeek)

? ? {

? ? ? ? var mask = 1 << (int)dayOfWeek;

? ? ? ? return (DaysOfWeek)Enum.ToObject(typeof(DaysOfWeek), mask);

? ? }

}

和用法:


? var days = DaysOfWeek.Sunday | DaysOfWeek.Friday;

? var day = DayOfWeek.Sunday;

? var ownDay = day.ToFlag();

? if (days.HasFlag(ownDay))

? ? ? // Do whatever;


查看完整回答
反對(duì) 回復(fù) 2023-04-29
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

public enum BitwiseDayOfWeek

{

    Sunday   = 1,

    Monday   = 2,

    Tuesday  = 4,

    Wednesday    = 8,

    Thursday     = 16,

    Friday   = 32,

    Saturday     = 64

}


public class Program

{

    public static void Main(string[] args)

    {

        BitwiseDayOfWeek scheduledDayOfWeek

            = BitwiseDayOfWeek.Saturday | BitwiseDayOfWeek.Sunday;


        // turn System.DayOfWeek (DateTime.Now.DayOfWeek) into BitwiseDayOfWeek

        BitwiseDayOfWeek currentDayOfWeek

            = (BitwiseDayOfWeek)Math.Pow(2, (double)DateTime.Now.DayOfWeek);


        // test if today is the scheduled day

        if ( (currentDayOfWeek & scheduledDayOfWeek) == currentDayOfWeek )

            Console.WriteLine(currentDayOfWeek);


        Console.WriteLine("---------------------");

        Console.ReadLine();

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-29
?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果您像這樣更改枚舉:


[Flags]

public enum DayOfWeek

{

    Sunday = 0,

    Monday = 1,

    Tuesday = 2,

    Wednesday = 3,

    Thursday = 4,

    Friday = 5,

    Saturday = 6

}

你可以試試這個(gè):


class Program

{

    public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)

    {

        int mineInt = (int)mine;

        int cSharpInt = (int)cSharp;

        return mineInt == cSharpInt;

    }


    static void Main(string[] args)

    {


        DateTime dateTime = DateTime.Now;

        DayOfWeek dayOfWeek = DayOfWeek.Sunday;


        bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);

        Console.WriteLine(areEqual);

        Console.ReadKey();

    }

}

如果你不能改變你的枚舉,你可以試試這個(gè):


class Program

{

    public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)

    {

        if (mine == DayOfWeek.Friday && cSharp == System.DayOfWeek.Friday       || 

            mine == DayOfWeek.Monday && cSharp == System.DayOfWeek.Monday       || 

            mine == DayOfWeek.Saturday && cSharp == System.DayOfWeek.Saturday   || 

            mine == DayOfWeek.Sunday && cSharp == System.DayOfWeek.Sunday       || 

            mine == DayOfWeek.Thursday && cSharp == System.DayOfWeek.Thursday   ||

            mine == DayOfWeek.Tuesday && cSharp == System.DayOfWeek.Tuesday     || 

            mine == DayOfWeek.Wednesday && cSharp == System.DayOfWeek.Wednesday)

            return true;


        return false;

    }


    static void Main(string[] args)

    {


        DateTime dateTime = DateTime.Now;

        DayOfWeek dayOfWeek = DayOfWeek.Tuesday;


        bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);

        Console.WriteLine(areEqual);

        Console.ReadKey();

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-29
  • 4 回答
  • 0 關(guān)注
  • 179 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)