Tag: C#的

可空types和三元运算符:为什么? 10:null`被禁止?

我刚碰到一个奇怪的错误: private bool GetBoolValue() { //Do some logic and return true or false } 然后,在另一种方法,像这样的东西: int? x = GetBoolValue() ? 10 : null; 很简单,如果方法返回true,则将10赋给Nullable int x。 否则,将null分配给可为空的 int。 但是,编译器抱怨: 错误1无法确定条件expression式的types,因为int和<null>之间没有隐式转换。 我疯了吗?

为什么我们在读取input后调用cin.clear()和cin.ignore()?

谷歌代码大学的C ++教程曾经有这样的代码: // Description: Illustrate the use of cin to get input // and how to recover from errors. #include <iostream> using namespace std; int main() { int input_var = 0; // Enter the do while loop and stay there until either // a non-numeric is entered, or -1 is entered. Note that // cin […]

使用C#读取CSV文件

我正在编写一个简单的导入应用程序,需要读取一个CSV文件,在DataGrid显示结果,并在另一个网格中显示CSV文件的损坏行。 例如,在另一个网格中显示短于5个值的行。 我试图这样做: StreamReader sr = new StreamReader(FilePath); importingData = new Account(); string line; string[] row = new string [5]; while ((line = sr.ReadLine()) != null) { row = line.Split(','); importingData.Add(new Transaction { Date = DateTime.Parse(row[0]), Reference = row[1], Description = row[2], Amount = decimal.Parse(row[3]), Category = (Category)Enum.Parse(typeof(Category), row[4]) }); } 但在这种情况下对arrays进行操作是非常困难的。 有没有更好的方法来分割值?

返回匿名types结果?

使用下面的简单示例,使用Linq to SQL返回多个表的结果的最佳方法是什么? 说我有两个表: Dogs: Name, Age, BreedId Breeds: BreedId, BreedName 我想把所有的狗都BreedName他们的BreedName 。 我应该让所有的狗使用这样的东西没有问题: public IQueryable<Dog> GetDogs() { var db = new DogDataContext(ConnectString); var result = from d in db.Dogs join b in db.Breeds on d.BreedId equals b.BreedId select d; return result; } 但是,如果我想要品种的狗,试试这个我有问题: public IQueryable<Dog> GetDogsWithBreedNames() { var db = new DogDataContext(ConnectString); var result […]

为什么malloc + memset比calloc慢?

众所周知, calloc和malloc是不同的,因为它初始化了分配的内存。 用calloc ,内存被设置为零。 用malloc ,内存不会被清除。 所以在日常工作中,我把calloc当作malloc + memset 。 顺便提一下,为了好玩,我为基准写了下面的代码。 结果是混乱。 代码1: #include<stdio.h> #include<stdlib.h> #define BLOCK_SIZE 1024*1024*256 int main() { int i=0; char *buf[10]; while(i<10) { buf[i] = (char*)calloc(1,BLOCK_SIZE); i++; } } 代码1的输出: time ./a.out **real 0m0.287s** user 0m0.095s sys 0m0.192s 代码2: #include<stdio.h> #include<stdlib.h> #include<string.h> #define BLOCK_SIZE 1024*1024*256 int main() { int i=0; char […]

你如何给C#自动属性一个默认值?

你如何给C#自动属性一个默认值? 我要么使用构造函数,要么恢复到旧的语法。 使用构造函数: class Person { public Person() { Name = "Default Name"; } public string Name { get; set; } } 使用正常的属性语法 (使用默认值) private string name = "Default Name"; public string Name { get { return name; } set { name = value; } } 有没有更好的办法?

如何将std :: string转换为小写?

我想将一个std::string转换为小写。 我知道函数tolower() ,但是在过去,我已经有了这个函数的问题,它是不是很理想,因为使用string将需要迭代每个字符。 有100%的时间有效的替代方法吗?

LINQ对DataTable的查询

我试图对DataTable对象执行一个LINQ查询,奇怪我发现在DataTable上执行这样的查询并不简单。 例如: var results = from myRow in myDataTable where results.Field("RowNo") == 1 select results; 这是不允许的。 我如何得到这样的工作? 我很惊讶LINQ查询不允许在数据表上!

什么是C#的正确版本号?

什么是C#的正确版本号? 什么时候出来? 为什么我找不到关于C#3.5的任何答案? 这个问题主要是为了帮助那些正在使用不正确的版本号寻找答案的人,例如“C#3.5”。 希望没有find错误版本号的答案的人会发现这个问题,然后再用正确的版本号再次search。

将void **转换为int – C的二维数组

我试图将一个void **指针投射到C中的一个int ** 2D数组 这里是我正在尝试使用的代码(删除所有多余的位): \*assume that i have a data structure called graph with some *element "void** graph" in it and some element "int order" */ void initialise_graph_data(graph_t *graph) { void **graph_data = NULL; int (*matrix)[graph->order]; size_t size = (graph->order * graph->order) * sizeof(int); graph_data = safe_malloc(size); /*safe malloc works fine*/ matrix = (int(*)[graph->order])graph_data; […]