public byte[][,] Shapes =
{
{
{1,1},
{1,1}
},
{
{1},
{1},
{1},
{1}
},
{
{0,0,1},
{1,1,1}
}
};
Run Code Online (Sandbox Code Playgroud)
我收到此错误:"数组初始化程序只能在变量或字段初始化程序中使用.请尝试使用新表达式."
我能做到这一点......
public class Shape
{
public byte[][,] Shapes;
public Shape()
{
Shapes = new byte[3][,];
Shapes[0] = new byte[2, 2];
Shapes[0][0, 0] = 1;
Shapes[0][0, 1] = 1;
Shapes[0][1, 0] = 1;
Shapes[0][1, 1] = 1;
Shapes[1] = new byte[1, 4];
Shapes[1][0, 0] = 1;
Shapes[1][0, 1] = 1;
Shapes[1][0, 2] = 1;
Shapes[1][0, 3] = 1;
}
} …Run Code Online (Sandbox Code Playgroud) 为了学习软件设计的新知识,我经常分析.NET框架源代码。
在今天的学习过程中,Microsoft.AspNetCore.Http.StatusCodes当我看到该类不是作为枚举实现,而是作为带有public const int字段(甚至不是属性)的静态类实现时,引起了我的注意。
当它看起来是一个完美的解决方案时,为什么不使用它enum来达到这个目的呢?这个设计决策背后的思考过程可能是什么?
我想我可能在这里遗漏了一些东西,我很乐意从这次经历中学到一些东西。
这是否有一个简短的手?
是否有匹配任何或null的通配符?
另一个是说它在零或一个匹配.
select [id], [word] from [FTSwordDef]
where [word] like 'system_'
or [word] like 'system'
Run Code Online (Sandbox Code Playgroud) 如果查看LIKE运算符的定义,您将看到有一个名为的可选参数ESCAPE,其描述如下:
是一个放在通配符前面的字符,表示通配符应该被解释为常规字符而不是通配符.escape_character是一个没有默认值的字符表达式,必须只计算一个字符.
因此,当您编写WHERE如下所示的子句时,您告诉SQL Server引擎将感叹号视为转义字符(就像'\'C#中的字符一样)以列出包含'30%'子字符串的注释:
WHERE comment LIKE '%30!%%' ESCAPE '!'
Run Code Online (Sandbox Code Playgroud)
现在这里让我感到困惑.已经可以通过将它们放在方括号内来转义通配符.为什么要引入这个ESCAPE论点呢?我猜这是有原因的.
编辑:我看到几个解释如何ESCAPE工作的答案.我知道这ESCAPE用于逃避通配符,但我也知道你可以使用方括号来逃避那些通配符.我只是想了解"ESCAPE方法"有什么,"方括号方法"没有.
编辑2:虽然Szymon的例子完全有效,但我觉得这不是引入ESCAPE的唯一原因.我当然可能错了,但我怀疑可能存在一些基于性能的原因,我不确定.Szymon,你可以达到如下相同的效果:
DECLARE @TEST_STRING VARCHAR(100) = 'ddd]eee';
SELECT 'MATCHED! (Szymon)' WHERE @TEST_STRING LIKE '%[abc!]]%' ESCAPE '!';
SELECT 'MATCHED! (Altern)' WHERE @TEST_STRING LIKE '%[abc]%' OR @TEST_STRING LIKE '%]%';
Run Code Online (Sandbox Code Playgroud)
作为旁注,您不需要转义结束方括号字符.
我有一个简单的控制台应用程序与以下正文:
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Run Code Online (Sandbox Code Playgroud)
这些是三种不同运行的输出:
635258949900018675
635258949900028676 // +10001
635258949900028676 // +0
635258949900038677 // +10001
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949937502423
635258949937512424 // +10001
635258949937512424 // +0
635258949937512424 // +0
635258949937512424 // +0
635258949937522425 // +10001
635258949937522425 // +0
635258949937522425 // +0
635258949937522425 // +0
635258949937522425 // +0
635258961813519906
635258961813529907 // +10001
635258961813529907 // +0
635258961813529907 // +0
635258961813529907 // …Run Code Online (Sandbox Code Playgroud) 在查看项目源代码时,我偶然发现了一个方法并且想知道一件事.从性能/内存/编译器的角度来看,以下两种方法是否完全相同?
public static string Foo(string inputVar)
{
string bar = DoSomething(inputVar);
return bar;
}
public static string Foo(string inputVar)
{
return DoSomething(inputVar);
}
Run Code Online (Sandbox Code Playgroud)
返回变量是由编译器自动创建的吗?
我们有一个非常复杂的项目,其中多个(可能是50个)不同的用户可能会修改几个文件。有时,为了理解并更改代码块,您需要联系编写该行/代码块/方法的人员。因此,您要做的就是遍历所有变更集,直到找到所需的变更集。
是否有内置功能可以直接在Visual Studio Team Explorer 2012中查找行/代码块/方法的作者?
如果没有,是否有具有此功能的第三方工具?
请考虑以下代码:
long longMaxValue = long.MaxValue;
decimal decimalMaxValue = decimal.MaxValue;
int a = (int)longMaxValue;
int b = (int)decimalMaxValue;
Run Code Online (Sandbox Code Playgroud)
问题1:为什么longMaxValue要将int结果转换为-1?
问题#2:为什么在以下异常中投射decimalMaxValue到int结果但是投射longMaxValue到int不?
对于Int32,值太大或太小.
我有一个简单的接口,两个类实现它:
public interface IMovable { }
public class Human : IMovable { }
public class Animal : IMovable { }
Run Code Online (Sandbox Code Playgroud)
以下泛型方法导致编译时错误: Cannot convert type 'Human' to 'T'
public static T DoSomething<T>(string typeCode) where T : class, IMovable
{
if (typeCode == "HUM")
{
return (T)new Human(); // Explicit cast
}
else if (typeCode == "ANI")
{
return (T)new Animal(); // Explicit cast
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当使用as关键字时,一切都很好:
public static T DoSomething<T>(string typeCode) where T : …Run Code Online (Sandbox Code Playgroud) 假设有一个列表:
var rawItems = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("A", "1"),
new KeyValuePair<string,string>("B", "2"),
new KeyValuePair<string,string>("C", "3")
};
Run Code Online (Sandbox Code Playgroud)
并且需要以下面的形式构造一个字符串:
A = 1,
B = 2,
C = 3
Run Code Online (Sandbox Code Playgroud)
使用的方法:
List<string> transformedItems = new List<string>();
rawItems.ForEach(item => transformedItems.Add(item.Key + " = " + item.Value));
string result = String.Join("," + Environment.NewLine, transformedItems.ToArray());
Run Code Online (Sandbox Code Playgroud)
如果有人能想到更优雅的方式,我会很高兴.
PS:不一定是"同一代码打包在一行"类型的解决方案,而是另一种方式.