小编val*_*ron的帖子

C#如何处理在结构上调用接口方法?

考虑:

interface I { void M(); }
struct S: I { public void M() {} }
// in Main:
S s;
I i = s;
s.M();
i.M();
Run Code Online (Sandbox Code Playgroud)

以及IL for Main:

.maxstack 1
.entrypoint
.locals init (
    [0] valuetype S s,
    [1] class I i
)

IL_0000: nop
IL_0001: ldloc.0
IL_0002: box S
IL_0007: stloc.1
IL_0008: ldloca.s s
IL_000a: call instance void S::M()
IL_000f: nop
IL_0010: ldloc.1
IL_0011: callvirt instance void I::M()
IL_0016: nop
IL_0017: ret
Run Code Online (Sandbox Code Playgroud)

First(IL_000a),S::M()使用值类型调用 …

.net c# struct cil interface

7
推荐指数
1
解决办法
210
查看次数

与"{}"的明显缓存相关的奇怪行为

今天我了解到Python缓存了表达式{},并在分配给变量时将其替换为新的空字典:

print id({})
# 40357936

print id({})
# 40357936

x = {}
print id(x)
# 40357936

print id({})
# 40356432
Run Code Online (Sandbox Code Playgroud)

我没有查看源代码,但我知道如何实现它.(可能当全局的引用计数{}递增时,全局{}会被替换.)

但请考虑一下:

def f(x):
    x['a'] = 1
    print(id(x), x)

print(id(x))
# 34076544

f({})
# (34076544, {'a': 1})

print(id({}), {})
# (34076544, {})

print(id({}))
# 34076544
Run Code Online (Sandbox Code Playgroud)

f修改全局字典而不使其被替换,并打印出修改后的字典.但除此之外f,尽管id是相同的,全球字典现在是空的!

怎么了??

python

5
推荐指数
1
解决办法
78
查看次数

标签 统计

.net ×1

c# ×1

cil ×1

interface ×1

python ×1

struct ×1