我最近在Resharper网站上看到了以下帖子.这是对双重检查锁定的讨论,并具有以下代码:
public class Foo
{
private static volatile Foo instance;
private static readonly object padlock = new object();
public static Foo GetValue()
{
if (instance == null)
{
lock (padlock)
{
if (instance == null)
{
instance = new Foo();
instance.Init();
}
}
}
return instance;
}
private void Init()
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
该帖子然后声称
如果我们假设Init()是用于初始化Foo状态的方法,那么由于内存模型不能保证读写顺序,上述代码可能无法按预期运行.因此,对Init()的调用实际上可能在变量实例处于一致状态之前发生.
这是我的问题:
我的理解是,.NET内存模型(至少从2.0开始)并没有要求instance被声明为volatile,因为lock它将提供一个完整的内存栅栏.是不是这样,还是我被误导了?
对于多个线程,是不是只对可观察的读/写重新排序?我的理解是,在一个线程上,副作用将是一致的顺序,并且lock就地防止任何其他线程观察到某些东西是不对的.我也在这里吗?
我正在使用Visual Studio 2010,WPF和C#4.0,当单击DataGrid中的单元格时,我得到以下异常:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll Additional information: A TwoWay or OneWayToSource binding cannot work on the read-only property 'Column2' of type 'VindecoderUI.AcesData'.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
<DataGrid AutoGenerateColumns="False" Height="190" Name="nadaDataGrid" Width="304" FrozenColumnCount="1000" ItemsSource="{Binding Source={StaticResource nadaDataCollection}}" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserSortColumns="False" AlternatingRowBackground="#3F000000" CanUserResizeRows="False" SelectionMode="Single" SelectionUnit="Cell"
SelectionChanged="dataGrid1_SelectionChanged" AreRowDetailsFrozen="True" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=NadaSeries}" Header="Nada Series" />
<DataGridTextColumn Binding="{Binding Path=NadaBS}" Header="Nada BS" />
<DataGridTextColumn Binding="{Binding Path=MSRP}" Header="MSRP" />
<DataGridTextColumn Binding="{Binding Path=GVWR}" Header="GVWR" />
<DataGridTextColumn Binding="{Binding Path=GCWR}" Header="GCWR" />
</DataGrid.Columns>
</DataGrid>
<Window.Resources> …Run Code Online (Sandbox Code Playgroud) 我有以下代码行
int i = (i = 20);
Run Code Online (Sandbox Code Playgroud)
并且它将i的值设置为20.现在我的问题是,两个语句是否相同?
int a = 0;
int i = (a = 20);
Run Code Online (Sandbox Code Playgroud)
和
int a = 0;
int i = a = 20;
Run Code Online (Sandbox Code Playgroud)
这两个语句都将值设置为i = 20和a = 20.有什么不同?
如果它们是相同的那么为什么有等价值的大括号?
在努力减少内存泄漏的过程中,我试图弄清楚,在为"DataContextChanged"事件或XAML用户控件上的"Loaded"事件添加处理程序后,即(UserControl.xaml.cs):
public MyUserControl()
{
InitializeComponent();
DataContextChanged += new DependencyPropertyChangedEventHandler(MyUserControl_DataContextChanged);
Loaded += new RoutedEventHandler(MyUserControl_Loaded);
}
Run Code Online (Sandbox Code Playgroud)
如果我需要删除它.WPF是否处理此问题,还是需要手动删除它们?
我创建了一个自定义Comparator来对字符串的ArrayList进行排序.我已通过调试器运行它并观察它正确比较和返回值.但是,我的数组没有排序.由于我是Java和Android的新手,可能还会有其他事情发生.
看了几个小时后,我无法弄清楚是什么......因为我一直在用这个网站回答这么多其他问题,我知道该去哪里!
Collections.sort(allWords, new Comparator<String>(){
public int compare(String o1, String o2) {
scoreWord sc1 = new scoreWord((String)o1);
scoreWord sc2 = new scoreWord((String)o2);
int i1 = sc1.getScore();
int i2 = sc2.getScore();
if ( i1 > i2 )
return 1;
return 0;
}
public boolean equals(String o1, String o2) {
scoreWord sc1 = new scoreWord((String)o1);
scoreWord sc2 = new scoreWord((String)o2);
int i1 = sc1.getScore();
int i2 = sc2.getScore();
if ( i1 == i2 )
return true;
return false;
}
});
Run Code Online (Sandbox Code Playgroud) 我有一个架构,我们将数据节点作为传递IEnumerable<BaseNode>.这一切都很好,但是在每个子类中我们都希望存储这些子类List<AnotherNode>,因为该类中的所有内容都创建并使用AnotherNode对象(我们有大约15个不同的子类).
使用更强类型列表的一个地方不起作用的是返回类型IEnumerable<BaseNode>且在.net 3.5中具有协方差限制的根类方法,该方法无法返回.(我们现在必须继续使用.net 3.5.)
但如果我有List<AnotherNode> data;并返回data.OfType<BaseNode>();- 这很好.所以这是我的问题.
由于所有数据都属于类型BaseNode- 这次通话的性能如何?因为替代方案是我必须在性能影响较小的地方施放 - 但这也是我们放弃一切知道它的类型的情况.
这是我试图获得一个独特的项目列表...
var queryResults = PatientList.Distinct();
PatientList = queryResults.ToList<SelectListItem>();
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我在这里没有得到明确的清单.
如何在javascript中获取OADate(OLE自动化日期)?我需要以double值的形式传递我的日期对象(到我的Web服务).
在c#中:
var d = DateTime.Now.ToOADate();
Run Code Online (Sandbox Code Playgroud)
js中的等价物是什么?
好吧,所以我在教我的女朋友一些c ++,她写了一个我觉得不行的程序,但确实如此.它访问数组中的另一个元素然后存在(例如,访问数组大小为5的数组[5]).这是缓冲区溢出的实例吗?我对它的想法是它在数组之后直接写入/访问内存,这是正确的吗?基本上我的问题是..为什么这样做?
#include <iostream>
using namespace std;
int main()
{
int size;
cout << "Please enter a size for the array." << endl;
cin >> size;
cout << endl;
cout << "There are " << size << " elements in this array." << endl;
cout << endl;
cout << endl;
cout << endl;
int array[size];
for (int counter = 1; counter <= size; counter++)
{
cout << "Please enter a value for element " << counter << "." << endl; …Run Code Online (Sandbox Code Playgroud) 我有一个启动函数调用一个函数,该函数根据设置是否成功返回一个布尔值.如果成功则为True,如果失败则为false.我想在新线程上启动该函数,然后检查函数的状态:这是代码.
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(StartAdapter));
thread.Start();
Run Code Online (Sandbox Code Playgroud)
我的问题是,在这种情况下我将如何检查startadapter方法的返回状态?因为我的朋友告诉我,我不会知道返回状态,因为它是在另一个线程上启动的,但是尝试:
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(StartAdapter));
thread.Start();
bool result = StartAdapter();
Run Code Online (Sandbox Code Playgroud)
会调用该函数两次,这也是我不想要的.有没有人对此有所了解?
在这种情况下,我将如何检查startadapter函数返回的布尔值?
.NET 3.5
我不确定这是否可行,但我希望能够让父类可以看到基类的字段/方法.
比方说,我有一个班级:
public class ExampleFile
{
private Stream _stream;
private long _baseoffset;
public ExampleFile(Stream input)
{
_stream = input;
_baseoffset = input.Position;
}
public void SeekTo(long offset)
{
_stream.Seek(offset + _baseoffset, SeekOrigin.Begin);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将该类用作另一个类的基础:
public class ExampleClass : ExampleFile
{
public ExampleClass(Stream input)
: base(input)
{
}
public byte[] GetSomething()
{
byte[] id = new byte[5];
SeekTo(2);
base._stream.Read(id, 0, 5);
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以让字段/方法ExampleFile只显示ExampleClass?
由于某些奇怪的原因,我无法访问此对象的属性即使我将其作为模型类型进行转换.有没有人知道为什么?(这可能很明显,但我对C#很陌生,所以请耐心等待!:o))
Users currentUser = new Users();
currentUser = (from x in db_tc.Users where x.Id == Convert.ToInt32(User.Identity.Name) select x);
Run Code Online (Sandbox Code Playgroud)
当我打电话时currentUser,我只能访问CRUD方法和一个List<Users>名为的属性usrList.我没有创建列表定义,所以我想这是自动创建的Entity框架的一部分.
我也尝试铸造currentUser与(Users)之前的实体查询,它并没有帮助的.
public static void Main()
{
Dictionary<string, double> values = new Dictionary<string, double>();
values.Add("a", 0.002);
values.Add("b", 0.003);
values.Add("c", 0.012);
// Summing iteratively.
double v1 = 615.0;
foreach (KeyValuePair<string, double> kp in values)
{
v1 += kp.Value;
}
Console.WriteLine(v1);
// Summing using the Sum method.
double v2 = 615.0;
v2 += values.Values.Sum();
Console.WriteLine(v2);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
当我看着它给人的615.01699999999994值调试器V1的价值,但对于V2它给人的615.017值.由于某种原因,Sum方法产生精确的结果,而迭代地对它们求和则不会.(当我打印这两个值时它们是相同的,但我认为这是由于WriteLine方法所做的一些舍入.)
有谁知道这里发生了什么?
c# ×10
linq ×3
c#-4.0 ×2
wpf ×2
.net ×1
.net-3.5 ×1
addition ×1
android ×1
arrays ×1
c++ ×1
collections ×1
comparator ×1
covariance ×1
datacontext ×1
double ×1
generics ×1
java ×1
javascript ×1
math ×1
sorting ×1
visibility ×1
wpf-controls ×1