如果你认为有可能获得空指针异常,你应该使用if语句来确保变量不为null,还是应该捕获异常?
我没有看到任何区别,因为你可以把你的逻辑处理if语句或catch块中的空指针,那么哪一个是最好的做法?
为什么new List<string>().ToString();返回以下内容:?
System.Collections.Generic.List`1[System.String]
Run Code Online (Sandbox Code Playgroud)
为什么它不会带回来System.Collections.Generic.List<System.String>.什么是奇怪的非C#语法?
我只是遇到了一些非常奇怪的东西:当你在一个值类型上使用Equals()方法时(如果这个方法当然没有被覆盖)你会得到一些非常慢的东西- 使用一对一比较字段反思!如:
public struct MyStruct{
int i;
}
(...)
MyStruct s, t;
s.i = 0;
t.i = 1;
if ( s.Equals( t )) /* s.i will be compared to t.i via reflection here. */
(...)
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么C#编译器不生成比较值类型的简单方法?像(在MyStruct的定义中):
public override bool Equals( Object o ){
if ( this.i == o.i )
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
编译器在编译时知道MyStruct的字段是什么,为什么它要等到运行时才能枚举MyStruct字段?
对我来说很奇怪.
谢谢 :)
补充:对不起,我只是意识到,当然,Equals它不是语言关键字而是运行时方法......编译器完全不知道这种方法.所以在这里使用反射是有意义的.
我创建了一个.NET Core项目,其中选择了WebApi模板,不包括身份验证.我想在其中添加ASP.NET标识以进行基于角色的授权.我怎么能做到这一点?
我正在使用SOS调试扩展DLL来检查String类型的内存布局.以下是结果.
!DSO
ESP/REG Object Name
0015EFC0 01c6b9cc System.String hello,world
Run Code Online (Sandbox Code Playgroud)
!做01c6b9cc
Name: System.String
MethodTable: 6de3f9ac
EEClass: 6db78bb0
Size: 36(0x24) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089>\mscorlib.dll
String: hello,world
Fields:
MT Field Offset Type VT Attr Value Name
6de42978 40000ed 4 System.Int32 1 instance 11 m_stringLength
6de41dc8 40000ee 8 System.Char 1 instance 68 m_firstChar
6de3f9ac 40000ef 8 System.String 0 shared static Empty
>> Domain:Value 00331488:01c61228 <<
Run Code Online (Sandbox Code Playgroud)
现在我想知道,字符串值"hello world"到底存储在哪里?
谢谢.
看看这个C代码:
int main()
{
unsigned int y = 10;
int x = -2;
if (x > y)
printf("x is greater");
else
printf("y is greater");
return 0;
}
/*Output: x is greater.*/
Run Code Online (Sandbox Code Playgroud)
我理解为什么输出是x更大,因为当计算机比较它们时,x被提升为无符号整数类型.当x被提升为无符号整数时,-2变为65534,绝对大于10.
但是为什么在C#中,等效代码会产生相反的结果呢?
public static void Main(String[] args)
{
uint y = 10;
int x = -2;
if (x > y)
{
Console.WriteLine("x is greater");
}
else
{
Console.WriteLine("y is greater");
}
}
//Output: y is greater.
Run Code Online (Sandbox Code Playgroud) 我正在遵循MSDN上的排队服务示例,但我不知道如何获取 的实例MonitorLoop,因为host.Services.GetRequiredService未定义。如何在 ASP.NET Core 6 中检索它?
或者更好的做法可能是使用后台范围的服务MonitorLoop?https://learn.microsoft.com/en-us/dotnet/core/extensions/scoped-service
public static class QueueServiceExtensions
{
public static void AddQueueService(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<MonitorLoop>();
services.AddHostedService<QueuedHostedService>();
services.AddSingleton<IBackgroundTaskQueue>(_ =>
{
if (!int.TryParse(configuration["QueueCapacity"], out var queueCapacity))
{
queueCapacity = 100;
}
return new DefaultBackgroundTaskQueue(queueCapacity);
});
// TODO: host.Services is undefined
MonitorLoop monitorLoop = host.Services.GetRequiredService<MonitorLoop>()!;
monitorLoop.StartMonitorLoop();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我的Program.cs的样子
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using QSGEngine.Web.QueueService;
using QSGEngine.Web.SignalR;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Logging
builder.Host.UseSerilog((context, …Run Code Online (Sandbox Code Playgroud) 我们知道System.Array是一个抽象类,无论DataType[]我们使用什么,运行时都会为我们创建一些具体的实现(虽然模糊不清).
请考虑以下代码段.
int[] someInts = { 1, 2, 3, 4 };
IList<int> collection = someInts;
collection.Clear();
Run Code Online (Sandbox Code Playgroud)
collection.Clear()抛出NotSupportedException,没有什么令人惊讶的.当我查看"StackTrace"时,我惊讶地发现它SZArrayHelper在调用堆栈顶部显示了一些奇怪的"类型" .
堆栈跟踪:
at System.SZArrayHelper.Clear[T]()//Note this.. How???
at TestApplication.Program.Main()
Run Code Online (Sandbox Code Playgroud)
怎么可能呢?我正在调用Clear()方法,int[]但是调用是如何进行的SZArrayHelper.Clear.请注意,这Clear是一个SZArrayHelper定义如下的实例方法.
private void Clear<T>()
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection"));
}
Run Code Online (Sandbox Code Playgroud)
谁创建了"SZArrayHelper"的实例,并注意到Clear方法是私有的.我很困惑地看到发生了什么.如果创建了一个"SZArrayHelper"实例并且Clear被调用,那么执行此调用的辅助方法应该出现在"StackTrace"中.但这不是这种情况.
有人可以解释一下幕后发生的事情吗?
注意:
int[]只是和例子,您可以使用任何类型的数组来模拟它.不仅Clear方法Add,Contains等等具有相同的行为..
我尝试使用反射器插件调试,这给出了相同的结果调试器显示直接调用SZArrayHelper.Clear<T>().
只是一个谷歌带我到这个.NET阵列,IList,通用算法,以及STL怎么样?.这有助于理解某种魔法落后但神秘仍然存在.
如何将double转换为int但确保无论十进制值如何都会向上舍入(只要十进制值不为0)?
为什么GetType().Name在字符串数组上进行调用Object[]而不是String[]?这似乎发生在任何元素类型中,例如Import-Csv会给你一个Object[]但每个元素都是一个PSCustomObject.
这是一个带数组的例子 String
$x = @('a','b','c')
$x[0].GetType().Name #String
$x.GetType().Name #Object[]
Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×3
powershell ×2
arrays ×1
asp.net ×1
asp.net-core ×1
c ×1
clr ×1
coercion ×1
collections ×1
generics ×1
int ×1
rounding ×1
struct ×1
types ×1