我有一类我有一个字符串字段。此字符串字段是常量,将来会在资源文件中移动,但暂时将保留在我们的类中。现在的情况是,我正在制作该类的数百个对象。所以我的问题是,哪种方法消耗的内存更少,为什么?
我应该让我的字符串变量 static
public class MyClass
{
public static string MyString = "My String";
}
Run Code Online (Sandbox Code Playgroud)我应该让我的字符串变量 const
public class MyClass
{
public const string MyString = "My String";
}
Run Code Online (Sandbox Code Playgroud)我想清楚我对LINQ的疑问.我的代码如下:
val collection = this.Employees.Where(emp => emp.IsActive)
foreach (var emp in collection)
{
// some stuff
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我写这样的代码:
foreach (var emp in this.Employees.Where(emp => emp.IsActive))
{
// some stuff
}
Run Code Online (Sandbox Code Playgroud)
将this.Employees.Where(emp => emp.IsActive)在每次迭代时执行还是只执行一次?
我正在主线程上进行一些繁重的计算,而这些计算无法在单独的线程上运行。
当这些计算正在运行时,我想在应用程序 UI 上显示一个“忙碌指示器”(即旋转小部件)。因此,我无法在主线程上显示繁忙指示器,因为在这些计算运行时 UI 被锁定。
为了解决这个问题,我尝试将忙碌指示器移动到单独的线程。在这篇文章的帮助下,我可以将忙碌指示器放在单独的线程上。但是,我无法与此线程通信以启动或停止繁忙指示器。
private HostVisual CreateBusyIndicatorOnWorkerThread()
{
// Create the HostVisual that will "contain" the VisualTarget
// on the worker thread.
HostVisual hostVisual = new HostVisual();
Thread thread = new Thread(new ParameterizedThreadStart(BusyIndicatorWorkerThread));
thread.ApartmentState = ApartmentState.STA;
thread.IsBackground = true;
thread.Start(hostVisual);
// Wait for the worker thread to spin up and create the VisualTarget.
s_event.WaitOne();
return hostVisual;
}
private static AutoResetEvent s_event = new AutoResetEvent(false);
private void BusyIndicatorWorkerThread(object arg)
{
// Create the VisualTargetPresentationSource and then …Run Code Online (Sandbox Code Playgroud) 我试图按照这篇文章,唯一的区别是我在后面的代码中创建和绑定控件.不幸的是它不起作用.这是我的示例代码:
public partial class ShellWindow
{
private static Visibility progressbarVisibility = Visibility.Collapsed;
public static Visibility ProgressbarVisibility
{
get { return progressbarVisibility; }
set
{
if (progressbarVisibility == value) return;
progressbarVisibility = value;
RaiseStaticPropertyChanged("ProgressbarVisibility");
}
}
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static void RaiseStaticPropertyChanged(string propName)
{
EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
if (handler != null)
handler(null, new PropertyChangedEventArgs(propName));
}
}
Run Code Online (Sandbox Code Playgroud)
我这样绑定
var binding = new Binding("ShellWindow.ProgressbarVisibility") { Mode = BindingMode.TwoWay };
progressbar = new CircularProgressBar ();
progressbar.SetBinding(VisibilityProperty,
binding);
Run Code Online (Sandbox Code Playgroud)
我想我错过了什么,但我不确定我错在哪里.任何帮助都会很棒.