Implicit conversion with null

I discovered a strange behaviour of my program and after futher analysis, I was able to find that there is probably something wrong either in my C# knowledge or somewhere else. I beleive it's my mistake but I cannot find an answer anywhere... public class B { public static implicit operator B(A values) { return null; } } public class A { } public class Program { s

用null进行隐式转换

我发现了程序中一个奇怪的行为,经过进一步分析后,我发现在我的C#知识或其他地方可能有问题。 我相信这是我的错误,但我无法在任何地方找到答案... public class B { public static implicit operator B(A values) { return null; } } public class A { } public class Program { static void Main(string[] args) { A a = new A(); B b = a ?? new B(); //b = null

not exact "7.13 The null coalescing operator" in C# 4.0 spec

in "7.13 The null coalescing operator" of C# 4.0 spec, it says Otherwise, if b has a type B and an implicit conversion exists from a to B , the result type is B. By my understand, it should be from A0 to B (A0 is the underlying type of A if A is a nullable type, or A otherwise). int? a=null; long b=5; then type for a??b is long, because an implicit conversion exists from int(namel

不准确“7.13 C#4.0规范中的空合并运算符”

它说,在C#4.0规范的“7.13零合并运算符”中 否则,如果b具有类型B并且存在从a到B的隐式转换,则结果类型是B. 据我了解,它应该是从A0到B (如果A是可空类型,A0是A的基础类型,否则A)。 int? a=null; long b=5; 那么输入a??b很长,因为存在从int(即A0)到long的隐式转换。 请帮助确认我的观点是否正确。 在C#3.0中它说: 否则,如果存在从A0到B的隐式转换,则结果类型为B. 在我的猜测中,它可能是C#4.0中的一

Understanding the null coalescing operator (??)

I have a custom WebControl which implements a .Value getter/setter returning a Nullable<decimal> It's a client-side filtered textbox (a subclass of TextBox with included javascript and some server side logic for setting/getting the value) Here is the getter & the setter from that control: public decimal? Value { get { decimal amount = 0; if (!decimal.Try

了解空合并运算符(??)

我有一个自定义WebControl ,它实现了一个返回Nullable <decimal>的.Value getter / setter 它是一个客户端过滤的文本框(包含JavaScript的TextBox的子类和用于设置/获取值的一些服务器端逻辑) 这是来自该控件的getter&setter: public decimal? Value { get { decimal amount = 0; if (!decimal.TryParse(this.Text, NumberStyles.Currency, null, out amount)) { ret

using coalescing null operator on nullable types changes implicit type

I would expect the next three lines of code to be the same: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now; var dateTimeWhatType = nullableDateTime ?? DateTime.Now; } In all cases, I assign nullableDateT

在可空类型上使用合并空操作符会改变隐式类型

我期望接下来的三行代码是一样的: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now; var dateTimeWhatType = nullableDateTime ?? DateTime.Now; } 在所有情况下,我将nullableDateTime分配给新变量。 我希望所有变

Why does the is operator return false when given null?

It seems to me that the is operator is a bit inconsistent. bool Test() { // Returns false, but should return true. return null is string; } One expects that the null value belongs to any reference (or nullable) type. And indeed, the C# language specification says something which supports this hypothesis, for example (6.1.6 Implicit reference conversions): The implicit reference conve

为什么is运算符在给定null时返回false?

在我看来,该is运营商是有点不一致。 bool Test() { // Returns false, but should return true. return null is string; } 人们希望null值属于任何引用(或可为空)的类型。 事实上,C#语言规范说明了一些支持该假设的内容,例如(6.1.6隐式引用转换): 隐式引用转换是: ... •从null文字到任何引用类型。 说明(7.10.10 is运算符)的is通过说表达式运算符开始(E is T)将导致真正的当从引用转换E到T存在,

How the right associative of null coalescing operator behaves?

null coalescing operator is right associative, which means an expression of the form first ?? second ??third is evaluated as first ?? (second ?? third) Based on the above rule, I think the following translation is not correct. From: Address contact = user.ContactAddress; if (contact == null) { contact = order.ShippingAddress; if (contact == null) { contact = user.

空合并运算符的正确关联如何表现?

空合并运算符是正确的关联,这意味着表单的形式 第一?? 第二??第三 被评估为 第一?? (第二??第三) 基于上述规则,我认为以下翻译是不正确的。 从: Address contact = user.ContactAddress; if (contact == null) { contact = order.ShippingAddress; if (contact == null) { contact = user.BillingAddress; } } 至: Address contact = user.ContactAddress ?? ord

Is it possible to detect if a Stream has been closed by the client?

A brief synopsis of the situation: I have a service that takes information in and sends replies out over Sockets. The connections are unsecured. I want to setup another service that can provide TLS to these connections - this new service would provide a single port and distribute the connections based on the client certificate provided. I don't want to use stunnel for a couple reasons, o

是否有可能检测客户端是否已关闭Stream?

情况简介: 我有一个服务,通过套接字发送回复信息。 连接不安全。 我想要设置另一项可以为这些连接提供TLS的服务 - 这项新服务将提供一个端口并根据提供的客户端证书分配连接。 我不想使用stunnel出于几个原因,其中之一就是每个接收端口需要一个转发端口。 我目前试图实现的解决方案: 本质上,我试图将一个SslStream(传入)与一个NetworkStream耦合(传出 - 可以是一个Socket,但是我将它放入一个NetworkStream中以

Using of threads to update labels after receiving string from bluetooth

Hi all i have a thread that is able to read data being received across from bluetooth stream. On the sender part i made it a while loop where count keeps on increasing + 1. I did a messagebox.show(test); and it works fine but when i do a label.text = test i get : "Control.Invoke must be used to interact with controls created on a separate thread." error. My follow code in C# : T

从蓝牙接收字符串后使用线程更新标签

大家好我有一个线程,可以读取从蓝牙流接收的数据。 在发件人部分,我做了一个while循环,其中count继续增加+ 1.我做了一个messagebox.show(test); 它工作正常,但是当我做一个label.text =测试我得到: “Control.Invoke必须用于与在单独的线程上创建的控件进行交互。” 错误。 我在C#中的跟随代码: 线程t =新线程(new ThreadStart(readStream)); t.Start(); public void readStream(){while(true){Strin

C# Locking, Properties & Permissions

I've been using lock on value type properties when multi-threaded access is required. Also, I've been meaning to become more diligent about applying proper access modifiers, especially in my library code that is starting to become useful in multiple projects. I've written some code and would like to request comments on the various strategies in it for properties and locking the memb

C#锁定,属性和权限

当需要多线程访问时,我一直在使用值类型属性。 另外,我一直希望能够更加努力地应用适当的访问修饰符,尤其是在我的库代码中,它正开始在多个项目中变得有用。 我已经编写了一些代码,并希望对其中的各种策略请求注释,并锁定它们所包装的成员变量。 谢谢。 using System; public class Program { static void Main(string[] args) { SomeValueType svt = new SomeValueType(); SomeReferenceType

Flex Specification yytext

I'm trying to build a flex specification for the k notation, for instance: 3k5 = 3500 . I have the following: [0-9]+{LETTER} { yyless(yyleng-1); yy_push_state(X_REAL); aux = atof(yytext); } <X_REAL>"k"[0-9]+ { yytext[0] = "." ; printf("%f", ((atof(yytext) * 10^(3)) + aux * 10^(3))); } However I get an error when trying to put "." in the first char of yytext: In me

Flex规范yytext

我试图为k表示法建立一个flex规范,例如: 3k5 = 3500 。 我有以下几点: [0-9]+{LETTER} { yyless(yyleng-1); yy_push_state(X_REAL); aux = atof(yytext); } <X_REAL>"k"[0-9]+ { yytext[0] = "." ; printf("%f", ((atof(yytext) * 10^(3)) + aux * 10^(3))); } 但是,当我试图放置"."时出现错误"." 在yytext的第一个字符中: 在成员函数'virtual int AtScanner :: yylex()'