Tag: null

空字符串上的ToString

为什么第二个产生exception而第一个产生exception呢? string s = null; MessageBox.Show(s); MessageBox.Show(s.ToString()); 更新 – 我能理解的例外,令人费解的一点(对我而言)是第一部分没有显示exception的原因。 这与Messagebox没有任何关系,如下图所示。 例如: string s = null, msg; msg = “Message is ” + s; //no error msg = “Message is ” + s.ToString(); //error 第一部分似乎是隐式地将null转换为空字符串。

将NULL值分配给SqlParameter的最佳方法

我在C#类方法中使用了许多可选的输入参数。 由于可选语法在未使用该参数时会创建值“0”,因此我在方法中调用的SQL插入命令会像这样插入。 但是,当不使用参数时,我需要命令插入NULL值而不是0。 在不使用大量“if”语句的情况下实现此目的的最佳方法是什么? 以下是我所指的代码。 是否有语法/某种命令允许我在SqlParameter声明中指定NULL值? public int batchInsert ( int id, int outcome, int input = 0, int add = 0, int update = 0, int delete = 0, int errors = 0, int warnings = 0 ) { string sts; if (outcome == 0) { sts = “S”; } else if (outcome == […]

如果参数为null,如何解决歧义?

编译以下代码将返回The call is ambiguous between the following methods or properties错误The call is ambiguous between the following methods or properties 。 如何解决它因为我无法将null显式转换为任何这些类? static void Main(string[] args) { Func(null); } void Func(Class1 a) { } void Func(Class2 b) { }

如果该行中的第一列为空,则entity framework为行返回null

我在我的Entity Framework模型中看到了一种奇怪的行为。 我有一个看起来像这样的查询: var rows = ( from alarm in context.Alarms join temp in context.ListDetails on alarm.ListDetailId equals temp.ListDetailId into entries from entry in entries.DefaultIfEmpty() join read in context.Reads on alarm.ReadId equals read.ReadId join plate in context.Images on alarm.ReadId equals plate.ReadId where alarm.IActive == 1 && ! alarm.TransmittedAlarm where read.IActive == 1 where plate.IActive == […]

将null传递给可接受的方法

Null对我来说是一种奇怪的数据类型,似乎它曾经是错误的使用,也许它的空指针错误我经常作为一个初学者,现在让我将任何null实例与某种邪恶联系起来! 无论如何我的问题是 在某些情况下,可以使用null作为参数吗? 例如,一个方法可能需要a和b来完成一项任务,但在某些情况下它可能只需要一个。 在那些奇怪的实例中为b解析为null,并检查是否(b == null)然后我们知道它叫什么? 还是我在这里的标志? 我必须承认,让我想知道这是否可以接受的是,所讨论的方法可以重叠,但是整个方法可能会有5或6行的差异。 这让我担心代码重复。

是否可以在C#中迭代方法的参数?

我一直在想能够做这样的事情是有用的,例如,检查空引用的参数并最终抛出exception。 这样可以节省一些打字,并且如果添加新参数,也会忘记添加检查。

为什么不能将nullebles声明为const?

[TestClass] public class MsProjectIntegration { const int? projectID = null; // The type ‘int?’ cannot be declared const // … } 为什么我不能拥有const int? ? 编辑:我想要一个可空的int作为const的原因是因为我只是用它来从数据库加载一些示例数据。 如果它为null我只是在运行时初始化样本数据。 这是一个非常快速的测试项目,显然我可以使用0或-1但是int? 感觉就像我想做的那样正确的数据结构。 readonly似乎是要走的路

为什么不将null评估为false?

在条件句中null为什么不评估为false的原因是什么? 我首先想到的是分配以避免使用=而不是==的错误,但编译器很容易不允许这样做。 if (someClass = someValue) // cannot convert someClass to bool. Ok, nice if (someClass) // Cannot convert someClass to bool. Why? if (someClass != null) // More readable? 我认为假设null意味着false是相当合理的。 还有其他语言也使用它,因为它没有我的错误。 编辑:我当然是指参考类型。 Daniel Earwicker关于赋值错误的一个很好的评论…这个编译没有警告,因为它评估为bool : bool bool1 = false, bool2 = true; if (bool1 = bool2) { // oops… false == true, and bool1 […]

为什么视图模型为null?

我有以下结构: Controller.cs public ActionResult PageMain(string param) { return View(); } PageMain.cs namespace project1.Models { public class PageMain { public DataTable dtable { get { // some code that returns a DataTable } } } } 最后在视图中: @using project1.Models @model PageMain var datatable = Model.dtable // but this is throwing an error since the model is […]

当方法尝试使用可以为null的字段时抛出哪个exception?

我实际上正致力于框架开发,这意味着需要一种非常强大的编码方法。 我遇到一个问题,我不知道我需要抛出哪个System.Exception派生类。 基本上就是这种情况,当我有一个类,其中的字段可以由构造函数初始化,并且具有使用这些字段的方法。 如果用户没有初始化这些字段,我必须抛出哪个例外? (这意味着它们为空) 这是一个例子: public class MyConnection { private Uri endpointUri; public Uri EndpointUri { get { return this.endpointUri; } set { this.endpointUri = value; } } public MyConnection() { } public MyConnection(Uri endpointUri) { this.endpointUri = endpointUri; } public FileStream GetFile() { if (this.endpointUri != null) { // My doer methods } […]