c#为结构赋值null [英] c# assign null to a struct

查看:95
本文介绍了c#为结构赋值null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定。我不确定这是酷还是变态,我需要第二个

意见;)

我定义了两个类如下:


/ ******************************************** /

公共密封班假人{

私人假人(){}

}


公共结构tnnt {


private Int32 val;

private Boolean def;


private tInt(Int32 pValue,Boolean pDefined ){

this.val = pValue;

this.def = pDefined;

}


public static bool operator ==(tInt lhs,Dummy rhs){

return!lhs.def;

}

public static bool operator !=(tInt lhs,Dummy rhs){

返回lhs.def;

}

公共静态隐式运算符tInt(Dummy pIn){

返回新的tInt(0,false);

}


public override bool Equals(object pOther){

if(pOther is tInt){

tInt wOther =(tInt)pOther;
if(this.def&& wOther.def){

返回this.val == wOther.val;

}否则{

抛出新的异常(未定义) ;);

}

}

返回false;

}


public override int GetHashCode(){

return base.GetHashCode();

}

}

/ ******************************************** /

通过这些定义,我现在可以将这个结构与null进行比较,好像它是一个参考类型。

例如,这可行。


/ ************************************** ****** /

tInt x = null;


if(x == null){

控制台。 WriteLine(x未定义);

}

/ ********************** ********************** /


哇。

我现在可以拿这个struct并使其行为就像一个int

也接受null作为值。

你怎么看?酷或变态?

Ok. I''m not sure whether this is cool or perverted, I need second
opinion ;)
I define two classes as follows:

/********************************************/
public sealed class Dummy {
private Dummy(){}
}

public struct tInt {

private Int32 val;
private Boolean def;

private tInt( Int32 pValue, Boolean pDefined ) {
this.val = pValue;
this.def = pDefined;
}

public static bool operator== (tInt lhs, Dummy rhs) {
return !lhs.def;
}
public static bool operator!= (tInt lhs, Dummy rhs) {
return lhs.def;
}
public static implicit operator tInt(Dummy pIn){
return new tInt( 0, false );
}

public override bool Equals( object pOther ) {
if ( pOther is tInt ) {
tInt wOther = (tInt)pOther;
if ( this.def && wOther.def) {
return this.val == wOther.val;
} else {
throw new Exception("not defined");
}
}
return false;
}

public override int GetHashCode() {
return base.GetHashCode();
}
}
/********************************************/

With these definitions I can now compare this struct to null as if it
were a reference type.
For example this works.

/********************************************/
tInt x = null;

if ( x == null ) {
Console.WriteLine( "x is undefined" );
}
/********************************************/

Wow.
I can now take this struct and make it behave just like an int that
also accepts null as a value.
What do you think? Cool or perverted?

推荐答案

我的第一个想法是对语言的冷静态度。

希望James Fisher看到这个 - 他想知道如何将
分配给int。当然,在我工作的程序中这样的事情会让我发疯 - 它必须非常好

记录下来,所以你没有得到你自己陷入困境(不是为了b $ b提到重载所需的所有操作员的工作

表现得更像真正的Int32)


-mike


" John" <福****** @ hotmail.com>在留言中写道

news:f3 ************************** @ posting.google.c om ...
My first thought is what a cool perversion of the language.
Hopefully James Fisher sees this - he was wanting to know how to
assign null to an int. Of course, things like this in a program I had
to work on would drive me crazy - it would have to VERY well
documented so that you didn''t get yourself into trouble (not to
mention the work of overloading all the operators needed to make it
behave more like a real Int32)

-mike

"John" <fu******@hotmail.com> wrote in message
news:f3**************************@posting.google.c om...
好的。我不确定这是酷还是变态,我需要第二个意见;)
我定义了两个类如下:

/ ******* ************************************* / / / / / / / / / / / / / / / / / >私人虚拟(){}
}
公共结构{
私人Int32 val;
私人布尔def;

private tInt(Int32 pValue,Boolean pDefined){
this.val = pValue;
this.def = pDefined;
}
公共静态bool运算符= =(tInt lhs,Dummy rhs){
return!lhs.def;
}
public static bool operator!=(tInt lhs,Dummy rhs){
return lhs.def ;
}
公共静态隐式运算符tInt(Dummy pIn){
返回新tInt(0,false);
}
公共覆盖bool Equals (对象pOther){
if(pOther is tnt){
t = ttnt =(tInt)pOther;
if(this.def&& wOther.def){
返回this.v al == wOther.val;
} else {
抛出新的异常(未定义);
}
}
返回false;
公共覆盖int GetHashCode(){
返回base.GetHashCode();
}
}
/ ******** ************************************ /

我可以用这些定义现在比较这个结构为null,好像
它是一个引用类型。
例如,这是有效的。

/ ************** ****************************** * /
t = x = null;

if( x == null){
Console.WriteLine(" x is undefined)" );
}
/ ************************************** ****** /

哇。
我现在可以使用这个结构,并使其行为就像一个int,
也接受null作为值。
您怎么看?酷或变态?
Ok. I''m not sure whether this is cool or perverted, I need second
opinion ;)
I define two classes as follows:

/********************************************/
public sealed class Dummy {
private Dummy(){}
}

public struct tInt {

private Int32 val;
private Boolean def;

private tInt( Int32 pValue, Boolean pDefined ) {
this.val = pValue;
this.def = pDefined;
}

public static bool operator== (tInt lhs, Dummy rhs) {
return !lhs.def;
}
public static bool operator!= (tInt lhs, Dummy rhs) {
return lhs.def;
}
public static implicit operator tInt(Dummy pIn){
return new tInt( 0, false );
}

public override bool Equals( object pOther ) {
if ( pOther is tInt ) {
tInt wOther = (tInt)pOther;
if ( this.def && wOther.def) {
return this.val == wOther.val;
} else {
throw new Exception("not defined");
}
}
return false;
}

public override int GetHashCode() {
return base.GetHashCode();
}
}
/********************************************/

With these definitions I can now compare this struct to null as if it were a reference type.
For example this works.

/********************************************/
tInt x = null;

if ( x == null ) {
Console.WriteLine( "x is undefined" );
}
/********************************************/

Wow.
I can now take this struct and make it behave just like an int that
also accepts null as a value.
What do you think? Cool or perverted?



我的第一个想法是对语言的冷静态度。

希望James Fisher看到这个 - 他我想知道如何将
分配给int。当然,在我工作的程序中这样的事情会让我发疯 - 它必须非常好

记录下来,所以你没有得到你自己陷入困境(不是为了b $ b提到重载所需的所有操作员的工作

表现得更像真正的Int32)


-mike


" John" <福****** @ hotmail.com>在留言中写道

news:f3 ************************** @ posting.google.c om ...
My first thought is what a cool perversion of the language.
Hopefully James Fisher sees this - he was wanting to know how to
assign null to an int. Of course, things like this in a program I had
to work on would drive me crazy - it would have to VERY well
documented so that you didn''t get yourself into trouble (not to
mention the work of overloading all the operators needed to make it
behave more like a real Int32)

-mike

"John" <fu******@hotmail.com> wrote in message
news:f3**************************@posting.google.c om...
好的。我不确定这是酷还是变态,我需要第二个意见;)
我定义了两个类如下:

/ ******* ************************************* / / / / / / / / / / / / / / / / / >私人虚拟(){}
}
公共结构{
私人Int32 val;
私人布尔def;

private tInt(Int32 pValue,Boolean pDefined){
this.val = pValue;
this.def = pDefined;
}
公共静态bool运算符= =(tInt lhs,Dummy rhs){
return!lhs.def;
}
public static bool operator!=(tInt lhs,Dummy rhs){
return lhs.def ;
}
公共静态隐式运算符tInt(Dummy pIn){
返回新tInt(0,false);
}
公共覆盖bool Equals (对象pOther){
if(pOther is tnt){
t = ttnt =(tInt)pOther;
if(this.def&& wOther.def){
返回this.v al == wOther.val;
} else {
抛出新的异常(未定义);
}
}
返回false;
公共覆盖int GetHashCode(){
返回base.GetHashCode();
}
}
/ ******** ************************************ /

我可以用这些定义现在比较这个结构为null,好像
它是一个引用类型。
例如,这是有效的。

/ ************** ****************************** * /
t = x = null;

if( x == null){
Console.WriteLine(" x is undefined)" );
}
/ ************************************** ****** /

哇。
我现在可以使用这个结构,并使其行为就像一个int,
也接受null作为值。
您怎么看?酷或变态?
Ok. I''m not sure whether this is cool or perverted, I need second
opinion ;)
I define two classes as follows:

/********************************************/
public sealed class Dummy {
private Dummy(){}
}

public struct tInt {

private Int32 val;
private Boolean def;

private tInt( Int32 pValue, Boolean pDefined ) {
this.val = pValue;
this.def = pDefined;
}

public static bool operator== (tInt lhs, Dummy rhs) {
return !lhs.def;
}
public static bool operator!= (tInt lhs, Dummy rhs) {
return lhs.def;
}
public static implicit operator tInt(Dummy pIn){
return new tInt( 0, false );
}

public override bool Equals( object pOther ) {
if ( pOther is tInt ) {
tInt wOther = (tInt)pOther;
if ( this.def && wOther.def) {
return this.val == wOther.val;
} else {
throw new Exception("not defined");
}
}
return false;
}

public override int GetHashCode() {
return base.GetHashCode();
}
}
/********************************************/

With these definitions I can now compare this struct to null as if it were a reference type.
For example this works.

/********************************************/
tInt x = null;

if ( x == null ) {
Console.WriteLine( "x is undefined" );
}
/********************************************/

Wow.
I can now take this struct and make it behave just like an int that
also accepts null as a value.
What do you think? Cool or perverted?



更好的是,我可以使用System.DBNull类而不是Dummy类。

DBNull。可以分配值,并将其与null进行比较。这一切

似乎太容易落到位......再次,哇。


Michael Mayer <先生***** @ charter.net>在消息中写道

新闻:#t ************** @ tk2msftngp13.phx.gbl ...
Even better, I can use the System.DBNull class instead of the Dummy class.
DBNull.Value could be assigned, and compared to as well as null. It all
seems to be falling into place too easily... Again, wow.

"Michael Mayer" <mr*****@charter.net> wrote in message
news:#t**************@tk2msftngp13.phx.gbl...
我的第一个想法是多么酷的语言变态。
希望詹姆斯费舍尔看到这一点 - 他想知道如何为int分配null。当然,在我工作的程序中这样的事情会让我发疯 - 它必须非常好地记录下来,以便你不会让自己陷入麻烦(不是为了提到重载所需的所有操作员的工作,使其更像是真实的Int32)

-mike

约翰 <福****** @ hotmail.com>在消息中写道
新闻:f3 ************************** @ posting.google.c om ...
My first thought is what a cool perversion of the language.
Hopefully James Fisher sees this - he was wanting to know how to
assign null to an int. Of course, things like this in a program I had
to work on would drive me crazy - it would have to VERY well
documented so that you didn''t get yourself into trouble (not to
mention the work of overloading all the operators needed to make it
behave more like a real Int32)

-mike

"John" <fu******@hotmail.com> wrote in message
news:f3**************************@posting.google.c om...
好的。我不确定这是酷还是变态,我需要第二个意见;)
我定义了两个类如下:

/ ******* ************************************* / / / / / / / / / / / / / / / / / >私人虚拟(){}
}
公共结构{
私人Int32 val;
私人布尔def;

private tInt(Int32 pValue,Boolean pDefined){
this.val = pValue;
this.def = pDefined;
}
公共静态bool运算符= =(tInt lhs,Dummy rhs){
return!lhs.def;
}
public static bool operator!=(tInt lhs,Dummy rhs){
return lhs.def ;
}
公共静态隐式运算符tInt(Dummy pIn){
返回新tInt(0,false);
}
公共覆盖bool Equals (对象pOther){
if(pOther is tnt){
t = ttnt =(tInt)pOther;
if(this.def&& wOther.def){
返回this.v al == wOther.val;
} else {
抛出新的异常(未定义);
}
}
返回false;
公共覆盖int GetHashCode(){
返回base.GetHashCode();
}
}
/ ******** ************************************ /

我可以用这些定义现在比较这个结构为null,好像
Ok. I''m not sure whether this is cool or perverted, I need second
opinion ;)
I define two classes as follows:

/********************************************/
public sealed class Dummy {
private Dummy(){}
}

public struct tInt {

private Int32 val;
private Boolean def;

private tInt( Int32 pValue, Boolean pDefined ) {
this.val = pValue;
this.def = pDefined;
}

public static bool operator== (tInt lhs, Dummy rhs) {
return !lhs.def;
}
public static bool operator!= (tInt lhs, Dummy rhs) {
return lhs.def;
}
public static implicit operator tInt(Dummy pIn){
return new tInt( 0, false );
}

public override bool Equals( object pOther ) {
if ( pOther is tInt ) {
tInt wOther = (tInt)pOther;
if ( this.def && wOther.def) {
return this.val == wOther.val;
} else {
throw new Exception("not defined");
}
}
return false;
}

public override int GetHashCode() {
return base.GetHashCode();
}
}
/********************************************/

With these definitions I can now compare this struct to null as if


是一个参考类型。
例如这是有效的。

/ ** ****************************************** * /
x = null;

if(x == null){
Console.WriteLine(" x is undefined)" );
}
/ ************************************** ****** /

哇。
我现在可以使用这个结构,并使其行为就像一个int,
也接受null作为值。
您怎么看?酷或变态?
were a reference type.
For example this works.

/********************************************/
tInt x = null;

if ( x == null ) {
Console.WriteLine( "x is undefined" );
}
/********************************************/

Wow.
I can now take this struct and make it behave just like an int that
also accepts null as a value.
What do you think? Cool or perverted?




这篇关于c#为结构赋值null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆