I have run into a problem that I'm not sure how to resolve. I have a method that contains a call from a service that populates a data table such as: private void GetCases() { try { //Setup our criteria Web_search_criteria myCriteria = new Web_search_criteria() { Keynum = 9, //Use Opening Date Key Range_start = "20100101", //01-01-2010
我遇到了一个我不确定如何解决的问题。 我有一个方法,其中包含来自填充数据表的服务的调用,例如: private void GetCases() { try { //Setup our criteria Web_search_criteria myCriteria = new Web_search_criteria() { Keynum = 9, //Use Opening Date Key Range_start = "20100101", //01-01-2010 Range_end = "20121223" //12-23-2013 };
I have two constructors which feed values to readonly fields. class Sample { public Sample(string theIntAsString) { int i = int.Parse(theIntAsString); _intField = i; } public Sample(int theInt) { _intField = theInt; } public int IntProperty { get { return _intField; } } private readonly int _intField; } One constructor
我有两个构造函数将值提供给只读字段。 class Sample { public Sample(string theIntAsString) { int i = int.Parse(theIntAsString); _intField = i; } public Sample(int theInt) { _intField = theInt; } public int IntProperty { get { return _intField; } } private readonly int _intField; } 一个构造函数直接接收值,另一个做一些计
Is it a good idea (from a design POV) to nest constructor calls for overloaded New or Factory style methods? This is mostly for simple constructors, where each overload builds on the previous one. MyClass( arg1 ) { _arg1 = arg1; _otherField = true; _color="Blue" } MyClass( arg1, arg2) : this(arg1) { _arg2 = arg2 } MyClass( arg1, arg2, arg3) : this(arg1, ar2) { _arg3
(从设计POV)到嵌套构造函数调用重载的New或Factory样式方法是一个好主意吗? 这主要是针对简单的构造函数,其中每个重载都建立在前一个重载上。 MyClass( arg1 ) { _arg1 = arg1; _otherField = true; _color="Blue" } MyClass( arg1, arg2) : this(arg1) { _arg2 = arg2 } MyClass( arg1, arg2, arg3) : this(arg1, ar2) { _arg3 = arg3; } 或者使用工厂方法: static NewInstance(arg1 ) {
During a code review with a Microsoft employee we came across a large section of code inside a try{} block. She and an IT representative suggested this can have effects on performance of the code. In fact, they suggested most of the code should be outside of try/catch blocks, and that only important sections should be checked. The Microsoft employee added and said an upcoming white paper warns
在与微软员工进行代码审查期间,我们遇到了try{}块中的大部分代码。 她和一位IT代表表示,这可能会影响代码的性能。 事实上,他们建议大部分代码应该在try / catch块之外,并且只应检查重要的部分。 微软员工补充说,即将发布的白皮书警告不正确的try / catch块。 我环顾四周,发现它会影响优化,但它似乎只适用于在范围之间共享变量的情况。 我并不是在询问代码的可维护性,或者甚至处理正确的例外情况(无疑,这些代码
如何在C#中生成随机的8个字符的字母数字字符串? I heard LINQ is the new black, so here's my attempt using LINQ: private static Random random = new Random(); public static string RandomString(int length) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); } (Note:
如何在C#中生成随机的8个字符的字母数字字符串? 我听说LINQ是新的黑色,所以这里是我使用LINQ的尝试: private static Random random = new Random(); public static string RandomString(int length) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); } (注意:Random类的使用
This question already has an answer here: Random number generator only generating one random number 7 answers You're making the Random instance in the method, which causes it to return the same values when called in quick succession. I would do something like this: private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden private string RandomString(int size)
这个问题在这里已经有了答案: 随机数发生器只产生一个随机数7个答案 你在方法中创建了Random实例,这使得它在快速连续调用时返回相同的值。 我会做这样的事情: private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden private string RandomString(int size) { StringBuilder builder = new StringBuilder(); char ch; for (int i = 0; i < size; i++)
I'm working with Hazard pointer in order to implement a lock-free linked list in C. I couldn't find any example code other than vary basics queues and stacks. The problem is I need to traverse the list, so my question is if I can change the value of a hazard pointer once is assigned. For example: t←Top while(true) { if t=null then return null *hp←t if Top!=t then
我正在使用Hazard指针来实现C中的无锁链表。除了各种基本队列和堆栈之外,我找不到任何示例代码。 问题是我需要遍历列表,所以我的问题是,如果一旦分配了危险指针的值,我就可以改变它的值。 例如: t←Top while(true) { if t=null then return null *hp←t if Top!=t then continue ... t←(t→next) //after this instruction pointer t will be still protected? } 最后,我根据原始文
Is it possible to apply an extension method to an interface? (C# question) That is for example to achieve the following: create an ITopology interface create an extension method for this interface (eg public static int CountNodes(this ITopology topologyIf) ) then when creating a class (eg MyGraph) which implements ITopology, then it would automatically have the Count Nodes extension. T
是否可以将扩展方法应用于接口? (C#问题) 这就是为了达到以下目的: 创建一个ITopology界面 为此接口创建一个扩展方法(例如public static int CountNodes(this ITopology topologyIf)) 那么当创建一个实现ITopology的类(例如MyGraph)时,它会自动拥有Count Nodes扩展。 这样,实现接口的类将不必具有设置的类名称以与扩展方法中定义的内容对齐。 当然他们可以; Linq的大部分是围绕接口扩展方法构建的。
Does C# have extension properties? For example, can I add an extension property to DateTimeFormatInfo called ShortDateLongTimeFormat which would return ShortDatePattern + " " + LongTimePattern ? No they do not exist in C# 3.0 and will not be added in 4.0. It's on the list of feature wants for C# so it may be added at a future date. At this point the best you can do is GetXXX
C#有扩展属性吗? 例如,我可以向DateTimeFormatInfo添加一个名为ShortDateLongTimeFormat的扩展属性,它将返回ShortDatePattern + " " + LongTimePattern ? 不,它们不存在于C#3.0中,并且不会在4.0中添加。 它位于C#的功能需求列表中,因此它可能会在未来的日期添加。 在这一点上,你可以做的最好的就是GetXXX风格的扩展方法。 不,他们不存在。 我知道C#团队正在考虑他们(或者至少是Eric Lippert
I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console. For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can go: Console.WriteBlueLine("This text is blue"); I tried this by adding a local, public static method, with Console as a 'this' parameter... but no di
我是C#中扩展方法的粉丝,但是还没有成功将扩展方法添加到静态类(如Console)。 例如,如果我想向控制台添加一个名为'WriteBlueLine'的扩展,以便我可以: Console.WriteBlueLine("This text is blue"); 我试图通过添加一个本地的公共静态方法,作为一个'这个'参数的控制台...但没有骰子! public static class Helpers { public static void WriteBlueLine(this Console c, string text) {