Имеет ли смысл присваивать Nullable свойству Tag любого элемента управления - C#
Формулировка задачи:
private void LeftDoorOpenCloseButton_Click(object sender, RoutedEventArgs e)
{
Duration duration = new Duration(TimeSpan.FromMilliseconds(800d));
if (!leftDoorImage.IsInitilizedPictures)
leftDoorImage.InitImages(ContentManagerProvider.LeftDoorBitmaps);
bool? opened = (bool?)(leftDoorImage.Tag ?? (leftDoorImage.Tag = new bool?(false)));
if (opened == false)
leftDoorImage.BeginAnimation(duration);
else
leftDoorImage.BeginAnimation(duration, reverse: true);
leftDoorImage.Tag = !opened;
}bool? opened = (bool?)(leftDoorImage.Tag ?? (leftDoorImage.Tag = new bool?(false)));
Решение задачи: «Имеет ли смысл присваивать Nullable свойству Tag любого элемента управления»
textual
Листинг программы
using System;
using System.Diagnostics;
namespace ConsoleApplication18
{
class Test
{
public object Tag { get; set; }
}
class Program
{
static void Main(string[] args)
{
const int n = 1000*1000;
var test = new Test();
bool trash;
var sw = Stopwatch.StartNew();
for (int i = 0; i < n; i++)
{
trash = test.Tag != null ? (bool) test.Tag : false;
test.Tag = !trash;
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
trash = (bool) test.Tag;
GC.KeepAlive(trash);
test = new Test();
bool? nullabletrash = null;
var sw2 = Stopwatch.StartNew();
for (int i = 0; i < n; i++)
{
nullabletrash = (bool?) (test.Tag ?? (test.Tag = new bool?(false)));
test.Tag = !nullabletrash;
}
sw2.Stop();
Console.WriteLine(sw2.Elapsed);
GC.KeepAlive(nullabletrash);
}
}
}