Tag: C#的

C ++中的静态方法

我在C ++中使用静态方法有点麻烦 例如.h: class IC_Utility { public: IC_Utility(); ~IC_Utility(); std::string CP_PStringToString( const unsigned char *outString ); void CP_StringToPString( std::string& inString, unsigned char *outString, short inMaxLength ); static void CP_StringToPString( std::string& inString, unsigned char *outString); void CP_StringToPString( FxString& inString, FxUChar *outString); }; 示例.cpp: static void IC_Utility::CP_StringToPString(std::string& inString, unsigned char *outString) { short length = inString.length(); if( […]

#ifdef和#if – 这是更好/更安全的方法来启用/禁用编译特定部分的代码?

这可能是一个风格问题,但我们的开发团队有一点分歧,我想知道是否有人对此有任何想法… 基本上,我们有一些debugging打印语句,我们在正常的开发过程中closures。 我个人更喜欢做以下事情: //—- SomeSourceFile.cpp —- #define DEBUG_ENABLED (0) … SomeFunction() { int someVariable = 5; #if(DEBUG_ENABLED) printf("Debugging: someVariable == %d", someVariable); #endif } 虽然有些团队喜欢以下内容: // #define DEBUG_ENABLED … SomeFunction() { int someVariable = 5; #ifdef DEBUG_ENABLED printf("Debugging: someVariable == %d", someVariable); #endif } …这些方法哪个更适合你,为什么? 我的感觉是,第一个是更安全的,因为总是有一些定义,没有任何危险可以破坏其他定义。

为什么不增加Nullable <int>抛出exception?

你能解释一下,为什么Console.WriteLine写空行( Console.WriteLine(null)给我编译错误),为什么没有NullReferenceException(即使a+=1不应该引发)? int? a = null; a++; // Why there is not NullReferenceException? Console.WriteLine(a); // Empty line

C#遍历一个枚举? (索引System.Array)

我有以下代码: // Obtain the string names of all the elements within myEnum String[] names = Enum.GetNames( typeof( myEnum ) ); // Obtain the values of all the elements within myEnum Array values = Enum.GetValues( typeof( myEnum ) ); // Print the names and values to file for ( int i = 0; i < names.Length; […]

如何修复“在C99模式外使用循环初始声明”GCC错误?

我试图解决3n + 1的问题 ,我有一个for循环,看起来像这样: for(int i = low; i <= high; ++i) { res = runalg(i); if (res > highestres) { highestres = res; } } 不幸的是,当我尝试使用GCC编译时遇到了这个错误: 3np1.c:15:error:'for'循环初始声明在C99模式之外使用 我不知道C99模式是什么。 有任何想法吗?

将列表转换为逗号分隔的string

我的代码如下: public void ReadListItem() { List<uint> lst = new List<uint>() { 1, 2, 3, 4, 5 }; string str = string.Empty; foreach (var item in lst) str = str + item + ","; str = str.Remove(str.Length – 1); Console.WriteLine(str); } 输出: 1,2,3,4,5 将List<uint>转换成逗号分隔string的最简单方法是什么?

如何在IntelliSense中的function在Visual Studio中的意见?

在Visual Studio和C#中,当使用诸如ToString()之类的内置函数时,IntelliSense会显示一个黄色框来解释它的function。 替代文字http://i44.tinypic.com/2r7rom8.jpg 替代文字http://i43.tinypic.com/5mcm4g.jpg 我怎么能为我写的function和属性呢?

构buildSystem.Net.HttpClient get的查询string

如果我想使用System.Net.HttpClient提交一个http get请求,似乎没有api来添加参数,这是正确的吗? 是否有任何简单的API可用于构build查询string,不涉及构build名称值集合和URL编码,然后最终连接它们? 我希望能使用像RestSharp的api(即AddParameter(..))

为什么我应该避免在C#中使用属性?

在他的优秀着作CLR Via C#中,杰弗里·里希特(Jeffrey Richter)表示,他不喜欢财产,并build议不要使用它们。 他提出了一些理由,但我不明白。 任何人都可以向我解释为什么我应该或不应该使用属性? 在具有自动属性的C#3.0中,这个改变了吗? 作为参考,我添加了Jeffrey Richter的观点: •财产可能是只读或只写; 字段访问总是可读写的。 如果你定义一个属性,最好同时提供get和set访问方法。 •属性方法可能会抛出exception; 字段访问永远不会引发exception。 •属性不能作为out或refparameter passing给方法; 一个领域可以。 例如,下面的代码将不能编译: using System; public sealed class SomeType { private static String Name { get { return null; } set {} } static void MethodWithOutParam(out String n) { n = null; } public static void Main() { // For […]

在条件expression式中检查可为空的bool的最佳方法(如果…)

我想知道什么是对可空布尔进行条件检查的最干净和可以理解的语法。 以下是好的还是坏的编码风格? 有没有办法更好地/更干净地expression条件? bool? nullableBool = true; if (nullableBool ?? false) { … } else { … } 尤其是if(nullableBool false)部分。 我不喜欢if (x.HasValue && x.Value)风格… (不知道这个问题之前是否被问过…找不到与search类似的东西)