我已经阅读了各地const
和static readonly
各个领域.我们有一些只包含常量值的类.用于我们系统中的各种事物.所以我想知道我的观察结果是否正确:
这些常量值是否应始终static readonly
适用于所有公开的内容?并且仅const
用于内部/受保护/私有值?
您有什么推荐的吗?我是否可能甚至不使用static readonly
字段,而是使用属性?
C#有扩展属性吗?
例如,我可以添加一个扩展属性来DateTimeFormatInfo
调用ShortDateLongTimeFormat
哪个会返回ShortDatePattern + " " + LongTimePattern
?
在博客文章中,我使用以下PHP来设置响应的内容类型:
header('content-type: application/json; charset=utf-8');
Run Code Online (Sandbox Code Playgroud)
我刚刚对该帖子发表评论说content-type
需要大写,Content-type
.它是否正确?它似乎适用于所有小写的我,我假设HTTP标题不区分大小写.或者它只是工作,因为浏览器很好?
我如何得到一个PriorityQueue
排序我希望它排序的东西?
查看某些内容(例如方法调用)接受代码的最精确方式是什么?
我猜的最容易和最快的是:
DateTime start = DateTime.Now;
{
// Do some work
}
TimeSpan timeItTook = DateTime.Now - start;
Run Code Online (Sandbox Code Playgroud)
但这有多精确?还有更好的方法吗?
可能重复:
在C#中取消订阅匿名方法
如何取消注册"匿名"事件处理程序
我最近发现我可以使用lambdas来创建简单的事件处理程序.我可以订阅像这样的点击事件:
button.Click += (s, e) => MessageBox.Show("Woho");
Run Code Online (Sandbox Code Playgroud)
但你怎么取消订阅呢?
这只是一个好奇的问题,我想知道是否有人有一个很好的答案:
在.NET Framework类库中,我们有两个方法:
public static IQueryable<TSource> Where<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate
)
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
Run Code Online (Sandbox Code Playgroud)
他们为什么用Func<TSource, bool>
而不是Predicate<TSource>
?好像Predicate<TSource>
只由List<T>
和Array<T>
,而Func<TSource, bool>
所使用的几乎所有Queryable
和Enumerable
方法和扩展方法...什么与怎么了?
我有这个代码:
private static $dates = array(
'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date
'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date
'close' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration closes
'early' => mktime( 0, 0, 0, 3, 19, 2009), // Date when early bird discount ends
);
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
解析错误:第19行/home/user/Sites/site/registration/inc/registration.class.inc中的语法错误,意外'(',期待')'
所以,我想我做错了什么......但是如果不是那样的话怎么办呢?如果我用常规字符串更改mktime内容,它就可以工作.所以,我知道我能做到这一点的那种像..
有人有指点吗?
我来自C#世界,所以对Java还没有太多经验.Eclipse刚刚告诉他们Date
已经弃用了.
Person p = new Person();
p.setDateOfBirth(new Date(1985, 1, 1));
Run Code Online (Sandbox Code Playgroud)
为什么?应该使用什么(特别是在上面的情况下)呢?
编写可以使用的xml文档时<see cref="something">something</see>
,当然可以使用.但是,如何引用具有泛型类型的类或方法?
public class FancyClass<T>
{
public string FancyMethod<K>(T value) { return "something fancy"; }
}
Run Code Online (Sandbox Code Playgroud)
如果我要在某处写xml文档,我将如何引用这个花哨的类?我该如何参考FancyClass<string>
?方法怎么样?
例如,在另一个类中,我想让用户知道我将返回一个实例FancyClass<int>
.我怎么能看到cref的东西呢?
c# ×6
.net ×2
java ×2
class ×1
constants ×1
date ×1
deprecated ×1
events ×1
func ×1
generics ×1
http ×1
http-headers ×1
lambda ×1
performance ×1
php ×1
predicate ×1
properties ×1
reference ×1
testing ×1