当我做了某乙以下..任何人修改一个(我以为这样做会克隆从人,将B).我也不知道如果更改Person a将在链接后更改Person b.由于我的代码现在,我只能在一个方向看到这个.
Person a = new Person() { head = "big", feet = "small" };
Person b = a;
b.head = "small"; //now a.head = "small" too
Run Code Online (Sandbox Code Playgroud)
现在如果我这样做了.人a变得完全分开.
Person b = new Person() { head = a.head, feet = a.feet };
Run Code Online (Sandbox Code Playgroud)
现在,将此行为与C#中的其他内容进行比较时,这种方法很有意义.但是,对于大型物体,这可能会非常烦人.
有没有办法简化这个?
如:
Person b = a.Values;
如何将2个列表传递给Parallel.ForEach?
例:
List<Person> a = new List<Person>() { new Person(), new Person(), new Person() };
List<Car> b = new List<Car>() { new Car(), new Car(), new Car() };
//PSEUDO CODE
Parallel.ForEach(a, b, (person, car) => {
//WORK ON person, WORK ON car
});
Run Code Online (Sandbox Code Playgroud)
我宁愿避免将Person和Car封装到Object容器中.这可能吗?
是否有更好/更准确/更严格的方法/方法来确定URL是否格式正确?
使用:
bool IsGoodUrl = Uri.IsWellFormedUriString(url, UriKind.Absolute);
Run Code Online (Sandbox Code Playgroud)
不抓住一切.如果我输入htttp://www.google.com并运行该过滤器,它就会通过.然后我NotSupportedException打电话给我WebRequest.Create.
这个错误的网址也会使它超过以下代码(这是我能找到的唯一其他过滤器):
Uri nUrl = null;
if (Uri.TryCreate(url, UriKind.Absolute, out nUrl))
{
url = nUrl.ToString();
}
Run Code Online (Sandbox Code Playgroud) 如何让VS ListView在"设计"面板中填充示例数据?
我很难过.我做了很多阅读和谷歌搜索,无法找到一个可靠的答案.在VS Design面板中显示虚假数据将大大加快主题,因为我不必每隔几次更改就调试它.
这是我的代码:
XAML:
<ListView DataContext="{Binding}"
Name="PeopleListView">
<ListView.ItemTemplate>
<DataTemplate DataType="Person">
<WrapPanel>
<TextBlock Text="{Binding Path=FirstNameView}"/>
<TextBlock Text="{Binding Path=LastNameView}" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
采集:
public class People : ObservableCollection<Person>
{
public People()
{
this.Add(new Person() { FirstName = "John", LastName = "Doe" });
this.Add(new Person() { FirstName = "John", LastName = "Doe" });
this.Add(new Person() { FirstName = "John", LastName = "Doe" });
}
}
Run Code Online (Sandbox Code Playgroud)
主要:
private People PList;
public MainWindow()
{
this.PList = new People();
PeopleListView.ItemsSource = …Run Code Online (Sandbox Code Playgroud) 假设我有50个我开始使用的请求BeginGetResponse.
如何查看每个请求的状态?
以及如何取消它(有时它们会挂起)?
以及如何在完成或取消所有请求后执行操作?
如何将IAsyncResult传递给AsyncCallback?
示例代码:
//Usage
var req = (HttpWebRequest)iAreq;
req.BeginGetResponse(new AsyncCallback(iEndGetResponse), req);
//Method
private void iEndGetResponse(IAsyncResult iA, bool iWantInToo) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
我想传递示例变量bool iWantInToo.我不知道如何添加它new AsyncCallback(iEndGetResponse).
我将如何通过在 Person 中选择的字典值对其进行排序?
示例:按“汽车”值降序排列“人”的顺序列表。
用那些奇特的 lambda/linq 方程之一可以做到吗?
public class People
{
List<Person> Persons { get; set; }
}
public class Person
{
public Dictionary<string, string> cars { get; set; }
public Dictionary<string, string> houses { get; set; }
public Dictionary<string, string> banks { get; set; }
}
................
People people = new People();
people.Persons = new List<Person>
{
new Person
{
cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } },
houses = new Dictionary<string, …Run Code Online (Sandbox Code Playgroud) private void tbLog_TextChanged(object sender, TextChangedEventArgs e)
{
//Get only NEW text added to Log
}
/*
LOG
old message...
old message...
old message...
old message...
NEW message...
NEW message...
NEW message...
NEW message...
NEW message...
*/
Run Code Online (Sandbox Code Playgroud)
如何仅从 TextBox 获取新文本?
有没有办法缩短我的 BackgroundWorker.CancellationPending 检查站?
例如,有没有办法return像下面的示例代码一样进行封装?:
//REAL CODE (CURRENTLY USE THIS)
if (this.TW.CancellationPending)
return;
//PSEUDO REPLACEMENT CODE
this.CkPt(CurrentMethod); //PSEUDO USAGE
//^^^ PARAMETER IS A REFERENCE TO THE CURRENT METHOD, SIMILAR TO `this` FOR AN OBJECT
//OR MAYBE AN EXTENSION METHOD WOULD LOOK CLEANER
CurrentMethod.CkPt(); //PSEUDO USAGE
private void CkPt(Method m) //PSEUDO METHOD
{
/*
POSSIBLY PERFORM OTHER CHECKPOINT TASKS HERE
*/
if (this.TW.CancellationPending)
m.return/*FROM METHOD THAT CALLED ME*/;
}
Run Code Online (Sandbox Code Playgroud)
我试图使这样的多检查点情况更具可读性:
//PSUEDO METHOD
//DO NOT TAKE THIS AS REPEATING CODE
//IT …Run Code Online (Sandbox Code Playgroud) 有没有办法在C#中执行此操作而不为每个var类型设置重载的新方法?
$box = !empty($toy) : $toy ? "";
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一方法是:
if (toy != null)
{
box += toy;
}
Run Code Online (Sandbox Code Playgroud)
或这个:
public string emptyFilter(string s) ...
public int emptyFilter(int i) ...
public bool emptyFilter(bool b) ...
public object emptyFilter(object o)
{
try
{
if (o != null)
{
return o.ToString();
}
else
{
return "";
}
}
catch (Exception ex)
{
return "exception thrown":
}
}
box += this.emptyFilter(toy);
Run Code Online (Sandbox Code Playgroud)
我基本上想检查以确保变量/属性设置/不为空/存在/有值/等等...并返回它或""没有一些像上面这样的代码的荒谬.
c# ×10
asynchronous ×2
wpf ×2
.net ×1
checkpoint ×1
data-binding ×1
foreach ×1
iasyncresult ×1
lambda ×1
linq ×1
object ×1
return ×1
sorting ×1
textbox ×1
uri ×1
url ×1
webrequest ×1
xaml ×1