我对此练习有一个问题:定义一个代表圆圈的类.保持pi值的常量定义类,以及在readonly中保持圆的颜色定义的变量.可能的颜色在枚举中定义.变量定义类以保持圆的半径和用于计算对象的周长和面积的函数.这就是我所做的:
class Circle
{
public const double PI = 3.14;
public readonly enum color { Black, Yellow, Blue, Green };
int radius;
public Circle(string Color,int radius)
{
this.radius = radius;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何将枚举选择放在构造函数中.谢谢你的帮助.
我有一个清单:
int list = { 1,1,2,3,4,4,5,7,7,7,10};
Run Code Online (Sandbox Code Playgroud)
现在我需要制作一个计算双数的程序.当前面的数字相同时,数字是两倍.我希望你明白.因此1是双倍,4是双打,我们在7,7,7中得到2双.
我声明字典如下:
private Dictionary<int, touchInformation> touchDictionary = new Dictionary<int, touchInformation>();
Run Code Online (Sandbox Code Playgroud)
我使用如下:
touchDictionary[touchID] = touchObject;
因此,touchDictionary将保留来自touchID的密钥.现在,我尝试使用字典找到最小密钥,但我不知道该怎么做.有什么建议吗?
请注意,C.Porawat
最初我在x86模式下遇到了stackoverflow异常.我注意到x64会优化尾递归,所以我切换到x64编译.并且它在调试模式下优雅地运行.但是当我尝试运行发布代码时...它再次抛出stackoverflow ..任何可能的原因?
我有一个枚举值列表:
public static readonly List<NotifyBy> SupportedNotificationMethods = new List<NotifyBy> {
NotifyBy.Email, NotifyBy.HandHold };
Run Code Online (Sandbox Code Playgroud)
我想将其输出为逗号分隔列表.(EG:"Email,Handhold")
这样做最干净的方法是什么?
我已经看过关于旋转2D阵列的其他帖子,但它并不是我想要的.我想要这样的东西
int[,] original= new int[4,2]
{
{1,2},
{5,6},
{9,10},
{13,14}
};
Run Code Online (Sandbox Code Playgroud)
我想把它变成这样,rotateArray = {{1,5,9,13},{2,6,10,14}}; 我想按列进行一些分析,而不是按行进行分析.
这有效,但有更简单的方法吗?
private static int[,] RotateArray(int[,] myArray)
{
int org_rows = myArray.GetLength(0);
int org_cols = myArray.GetLength(1);
int[,] myRotate = new int[org_cols, org_rows];
for (int i = 0; i < org_rows; i++)
{
for(int j = 0; j < org_cols; j++)
{
myRotate[j, i] = myArray[i, j];
}
}
return myRotate;
}
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来迭代c#中的列?
乙
我有一个方法返回DataSet表的List
public static List<string> GetListFromDataTable(DataSet dataSet, string tableName, string rowName)
{
int count = dataSet.Tables[tableName].Rows.Count;
List<string> values = new List<string>();
// Loop through the table and row and add them into the array
for (int i = 0; i < count; i++)
{
values.Add(dataSet.Tables[tableName].Rows[i][rowName].ToString());
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以动态设置列表的数据类型,并且这个方法适合所有数据类型,所以我可以在调用此方法时指定它应该是List<int> List<string>或者List<AnythingILike>?
另外,在声明方法时返回类型是什么?
先谢谢,布雷特
List<string> list = new List<string>() {"a", "b", "c"};
IEnumerable<string> enumerable = list;
int c1 = list.Count;
int c2 = list.Count();
int c3 = enumerable.Count();
Run Code Online (Sandbox Code Playgroud)
最后3个陈述之间在性能和实施方面是否存在差异?将list.Count()表现更差或相同list.Count,如果引用类型IEnumerable<string>是否重要?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
object[] obj = new object[3];
obj[0] = new object();
obj[1] = "some string";
obj[2] = 10;
string[] strings = new string[] { "one", "two", "three" };
obj = strings; //---> No Error here, Why ?
int[] ints = new int[] { 1, 2, 3 };
obj = ints; /*-> Compiler error - Cannot implicitly convert type 'int[]' to 'object[]', Why ?*/
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在执行上述步骤时遇到编译器错误.但是,在上一步中,没有错误.有人能解释一下这种行为吗?我正在使用VS 2010.
编辑 - 为了完整性,再次,这将无法编译 …