标签: nullable

如何将枚举设置为null

我有一个枚举

string name;

public enum Color
{
  Red,
  Green,
  Yellow
}
Run Code Online (Sandbox Code Playgroud)

如何在加载时将其设置为NULL.

name = "";
Color color = null; //error
Run Code Online (Sandbox Code Playgroud)

编辑:我的不好,我没有正确解释.但所有与可空的相关的答案都是完美的.我的情况是如果,我已经在类中使用其他元素(例如名称等)获取/设置枚举.在页面加载时,我初始化类并尝试将值默认为null.这是场景(代码在C#中):

namespace Testing
{
    public enum ValidColors
    {
        Red,
        Green,
        Yellow
    }

    public class EnumTest
    {
        private string name;
        private ValidColors myColor;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public ValidColors MyColor
        {
            get { return myColor; }
            set { myColor = value; }
        }

    }

    public partial class _Default : …
Run Code Online (Sandbox Code Playgroud)

c# null enums nullable

172
推荐指数
5
解决办法
20万
查看次数

Laravel迁移变为使列可以为空

我用unsigned创建了一个迁移user_id.如何user_id在新迁移中编辑以进行编辑nullable()

Schema::create('throttle', function(Blueprint $table)
{
    $table->increments('id');
    // this needs to also be nullable, how should the next migration be?
    $table->integer('user_id')->unsigned();
}
Run Code Online (Sandbox Code Playgroud)

nullable laravel eloquent laravel-5 laravel-migrations

172
推荐指数
9
解决办法
17万
查看次数

类型'string'必须是非可空类型才能在泛型类型或方法'System.Nullable <T>'中将其用作参数T.

为什么我会得到错误"类型'字符串'必须是不可为空的值类型才能在泛型类型或方法'System.Nullable'中将它用作参数'T'?"

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Universe;

namespace Universe
{
    public class clsdictionary
    {
      private string? m_Word = "";
      private string? m_Meaning = "";

      string? Word { 
          get { return m_Word; }
          set { m_Word = value; }
      }

      string? Meaning { 
          get { return m_Meaning; }
          set { m_Meaning = value; }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# nullable

167
推荐指数
4
解决办法
13万
查看次数

如何将PG列更改为NULLABLE TRUE?

如何使用Postgres完成此操作?我已经尝试了下面的代码,但它不起作用:

ALTER TABLE mytable ALTER COLUMN mycolumn BIGINT NULL; 
Run Code Online (Sandbox Code Playgroud)

sql postgresql nullable

166
推荐指数
1
解决办法
8万
查看次数

检查类型是否为Nullable的正确方法

为了检查a Type(propertyType)是否可以为空,我正在使用:

bool isNullable =  "Nullable`1".Equals(propertyType.Name)
Run Code Online (Sandbox Code Playgroud)

有没有办法避免使用魔法字符串?

c# generics nullable

162
推荐指数
2
解决办法
6万
查看次数

在Kotlin中,处理可空值,引用或转换它们的惯用方法是什么

如果我有一个可空类型Xyz?,我想引用它或将其转换为非可空类型Xyz.在Kotlin这样做的惯用方法是什么?

例如,此代码出错:

val something: Xyz? = createPossiblyNullXyz()
something.foo() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Xyz?"
Run Code Online (Sandbox Code Playgroud)

但是,如果我首先检查null是允许的,为什么?

val something: Xyz? = createPossiblyNullXyz()
if (something != null) {
    something.foo() 
}
Run Code Online (Sandbox Code Playgroud)

假设我确定它确实从未如此,如何null在不需要if检查的情况下更改或处理值null?例如,在这里,我从地图中检索一个我可以保证存在的值而结果get()不是null.但是我有一个错误:

val map = mapOf("a" to 65,"b" to 66,"c" to 67)
val something = map.get("a")
something.toLong() // Error: "Only safe (?.) or non-null asserted (!!.) calls …
Run Code Online (Sandbox Code Playgroud)

nullable non-nullable kotlin

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

Nullable类型问题?:条件运算符

有人可以解释为什么这在C#.NET 2.0中有效:

    Nullable<DateTime> foo;
    if (true)
        foo = null;
    else
        foo = new DateTime(0);
Run Code Online (Sandbox Code Playgroud)

......但这不是:

    Nullable<DateTime> foo;
    foo = true ? null : new DateTime(0);
Run Code Online (Sandbox Code Playgroud)

后一种形式给我一个编译错误"无法确定条件表达式的类型,因为'<null>'和'System.DateTime'之间没有隐式转换."

并不是说我不能使用前者,但第二种风格与我的其余代码更加一致.

c# generics nullable conditional-operator

154
推荐指数
3
解决办法
6万
查看次数

PHP7中可为空的返回类型

PHP 7引入了返回类型声明.这意味着我现在可以指示返回值是某个类,接口,数组,可调用或新的可阻塞标量类型之一,这对于函数参数是可能的.

function returnHello(): string {
    return 'hello';
}
Run Code Online (Sandbox Code Playgroud)

通常情况下,值并不总是存在,并且您可能返回某种类型的某些内容,或者返回null.虽然您可以通过将其默认值设置为null(DateTime $time = null)来使参数可为空,但似乎没有办法为返回类型执行此操作.确实如此,或者我不知道怎么做?这些不起作用:

function returnHello(): string? {
    return 'hello';
}

function returnHello(): string|null {
    return 'hello';
}
Run Code Online (Sandbox Code Playgroud)

php nullable return-type type-hinting php-7

148
推荐指数
2
解决办法
7万
查看次数

Objective-C中nullable,__nullable和_Nullable之间的区别

使用Xcode 6.3,引入了新的注释,以便更好地表达Objective-C中API的意图(并确保更好的Swift支持).那些注释当然是nonnull,nullablenull_unspecified.

但是使用Xcode 7,会出现很多警告,例如:

指针缺少可为空类型说明符(_Nonnull,_Nullable或_Null_unspecified).

除此之外,Apple使用另一种类型的可为空性说明符,标记其C代码(源代码):

CFArrayRef __nonnull CFArrayCreate(?CFAllocatorRef __nullable allocator, const void * __nonnull * __nullable values, CFIndex numValues, const CFArrayCallBacks * __nullable callBacks);

总而言之,我们现在拥有这3种不同的可空性注释:

  • nonnull,nullable,null_unspecified
  • _Nonnull,_Nullable,_Null_unspecified
  • __nonnull,__nullable,__null_unspecified

即使我知道为什么以及在哪里使用哪个注释,我也会对我应该使用哪种类型的注释,在哪里以及为什么感到困惑.这是我可以收集的:

  • 对于我应该使用性质nonnull,nullable,null_unspecified.
  • 对于我应该使用方法参数nonnull,nullable,null_unspecified.
  • 对于C方法我应该使用__nonnull,__nullable,__null_unspecified.
  • 对于其他情况,如双指针,我应该使用_Nonnull,_Nullable,_Null_unspecified. …

nullable objective-c objective-c-nullability

144
推荐指数
4
解决办法
4万
查看次数

无法确定条件表达式的类型,因为'int'和<null>之间没有隐式转换

为什么这不编译?

int? number = true ? 5 : null;
Run Code Online (Sandbox Code Playgroud)

无法确定条件表达式的类型,因为'int'和<null>之间没有隐式转换

c# nullable

141
推荐指数
4
解决办法
6万
查看次数