啊,你不喜欢一个好的三元滥用吗?:)考虑以下表达式:
true ? true : true ? false : false
Run Code Online (Sandbox Code Playgroud)
对于那些现在完全感到困惑的人,我可以告诉你,这个评估是真的.换句话说,它等同于:
true ? true : (true ? false : false)
Run Code Online (Sandbox Code Playgroud)
但这可靠吗?我能否确定在某些情况下不会出现这种情况:
(true ? true : true) ? false : false
Run Code Online (Sandbox Code Playgroud)
有些人可能会说 - 好吧,只需添加括号或完全不使用它 - 毕竟,众所周知,三元运算符是邪恶的!
当然它们是,但在某些情况下它们确实有意义.对于好奇的 - 我正在拧干通过一系列属性比较两个对象的代码.如果我像这样冷写它会很好:
obj1.Prop1 != obj2.Prop1 ? obj1.Prop1.CompareTo(obj2.Prop1) :
obj1.Prop2 != obj2.Prop2 ? obj1.Prop2.CompareTo(obj2.Prop2) :
obj1.Prop3 != obj2.Prop3 ? obj1.Prop3.CompareTo(obj2.Prop3) :
obj1.Prop4.CompareTo(obj2.Prop4)
Run Code Online (Sandbox Code Playgroud)
简洁明了.但它确实取决于三元运算符的相关性,就像第一种情况一样.括号只会使意大利面条脱离它.
那么 - 这是指定的吗?我找不到它.
只是想查看是否有更优雅的方法来使用Linq完成此任务.为简洁起见,我大大简化了代码.我会在一分钟内介绍我的理由,但它是这样的:
(from t in doc.Descendants(p + "Task")
where t.Element(p + "Name") != null
select new {
FirstName = t.FirstName,
LastName = t.LastName,
FullName = FirstName + " " + LastName // Error!
}
Run Code Online (Sandbox Code Playgroud)
是的,我知道执行FullName = t.FirstName +""+ t.LastName会很简单,但让我们想象一下,FirstName和LastName是大丑的内联计算,而不是简单的变量.所以FullName = [大难看的计算1] + [大难看的计算2].因此,根据DRY的精神,有没有更好的方法可以做到这一点?我的第一个想法是编写一个给我FirstName和LastName的函数.但还有更好的东西吗?
我正在寻找http://docs.python.org/library/socketserver.html来尝试使用python中的socketserver处理异步请求.在最底部有一个例子,但它没有意义.它说您使用端口0分配任意未使用的端口.但是,如果客户端不在同一个程序中,您如何知道客户端使用哪个端口?我不太明白如何使这个有用.
我正在使用WPF中的图像管理应用程序来显示许多图像,并允许用户在文件系统中移动它们.我遇到的问题是显示带有<Image>元素的文件似乎保持文件打开,因此尝试移动或删除文件失败.有没有办法手动要求WPF卸载或释放文件,以便可以移动?或者是否有一种显示不保持文件打开的图像的方法?查看器Xaml如下:
<ListBox x:Name="uxImages" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="4">
<Image Source="{Binding}" Width="150" Height="150"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我需要将我的应用程序添加到启动,因此需要使用安装路径设置注册表项.我通过Visual Studio安装项目完成了这项工作,但无法通过ClickOnce部署弄清楚如何实现.
任何人都知道在使用ClickOnce进行安装时如何设置注册表项?
我只找到了一种相反的方法:从int列表或数组创建一个逗号分隔的字符串,但不是如何将输入转换string str = "1,2,3,4,5";为数组或int列表.
这是我的实现(灵感来自Eric Lippert的这篇文章):
public static IEnumerable<int> StringToIntList(string str)
{
if (String.IsNullOrEmpty(str))
{
yield break;
}
var chunks = str.Split(',').AsEnumerable();
using (var rator = chunks.GetEnumerator())
{
while (rator.MoveNext())
{
int i = 0;
if (Int32.TryParse(rator.Current, out i))
{
yield return i;
}
else
{
continue;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你认为这是一个好方法还是更简单,甚至可能是内置方式?
编辑:对不起任何混乱,但该方法需要处理等无效的输入"1,2,,,3"或"###, 5,"通过跳过它等等.
是否有编译器指令,以忽略"从不兼容的指针类型初始化"警告Hardware_MouseDrivers_GPM_Methods和Hardware_MouseDrivers_DevInput_Methods?但是,全局关闭警告不是一种选择.
#include <stdio.h>
/* Mouse driver interface */
typedef struct _Hardware_MouseDriver {
int (*open)(void*, char *);
int (*close)(void*);
int (*poll)(void*);
} Hardware_MouseDriver;
/* GPM */
typedef struct _Hardware_MouseDrivers_GPM {
char *path;
} Hardware_MouseDrivers_GPM;
static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path);
static int Hardware_MouseDrivers_GPM_Close(Hardware_MouseDrivers_GPM *this);
static int Hardware_MouseDrivers_GPM_Poll(Hardware_MouseDrivers_GPM *this);
static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path) {
printf("GPM: Opening %s...\n", path);
this->path = path;
}
static int Hardware_MouseDrivers_GPM_Close(Hardware_MouseDrivers_GPM *this) {
printf("GPM: Closing %s...\n", this->path);
}
static int Hardware_MouseDrivers_GPM_Poll(Hardware_MouseDrivers_GPM *this) …Run Code Online (Sandbox Code Playgroud) 我一直在寻找在iPhone上屏蔽图像的方法.我遇到了这个解决方案
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
这对静止图像很有用.我想要做的是在UIImageView中屏蔽动画.从我所看到的,我不认为这是可能的,同时也实现了不错的帧速率.
这让我想问以下是否可能,我可以"剪辑"UIImageView中的图像吗?即不重新调整UIImageView的大小到图像的大小,所以一些部分被砍掉?
我有一个项目,需要打印一个包含许多行的HTML表.
我的问题是表格在多页上打印的方式.它有时会减少一半,使其无法读取,因为一半位于页面的边缘,其余部分打印在下一页的顶部.
我能想到的唯一合理的解决方案是使用堆叠的DIV而不是表格,并在需要时强制分页.但在完成整个更改之前,我认为我之前可以问过这个问题.
有了GridView客户端控件ID,我想得到一些关于如何计算GridView行的想法,或者至少能够判断Gridview上是否至少有一行限制是我只能使用客户端代码(这种情况下的JavaScript)
c# ×3
.net ×1
animation ×1
asp.net ×1
asynchronous ×1
c ×1
clickonce ×1
css ×1
file ×1
gcc ×1
gridview ×1
html ×1
html-table ×1
image ×1
installation ×1
iphone ×1
javascript ×1
linq ×1
mask ×1
networking ×1
printing ×1
python ×1
python-2.7 ×1
uiimageview ×1
wpf ×1