最新cba赛程 chatgpt优秀

在日常学习、工作或生活中,大家总少不了接触作文或者范文吧,通过文章可以把我们那些零零散散的思想,聚集在一块。相信许多人会觉得范文很难写?接下来小编就给大家介绍一下优秀的范文该怎么写,我们一起来看一看吧。

cba赛程 chatgpt篇一

引导语:c#适合为独立和嵌入式的系统编写程序,从使用复杂操作系统的大型系统到特定应用的小型系统均适用。以下是小编整理的c#转换关键词explicit的使用,欢迎参考阅读!

explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。例如,在下面的示例中,此运算符将名为 fahrenheit 的类转换为名为 celsius 的`类:

c#

// must be defined inside a class called farenheit:

public static explicit operator celsius(fahrenheit f)

{

 return new celsius((5.0f / 9.0f) * (s - 32));

}

可以如下所示调用此转换运算符:

c#

fahrenheit f = new fahrenheit(100.0f);

("{0} fahrenheit", s);

celsius c = (celsius)f;

转换运算符将源类型转换为目标类型。源类型提供转换运算符。与隐式转换不同,必须通过强制转换的方式来调用显式转换运算符。如果转换操作可能导致异常或丢失信息,则应将其标记为 explicit。这可以防止编译器无提示地调用可能产生无法预见后果的转换操作。

省略此强制转换将导致编译时错误 编译器错误 cs0266。

下面的示例提供 fahrenheit 和 celsius 类,它们中的每一个都为另一个提供显式转换运算符。

c#

class celsius

{

 public celsius(float temp)

 {

   degrees = temp;

 }

 public static explicit operator fahrenheit(celsius c)

 {

   return new fahrenheit((9.0f / 5.0f) * s + 32);

 }

 public float degrees

 {

   get { return degrees; }

 }

 private float degrees;

}

class fahrenheit

{

 public fahrenheit(float temp)

 {

   degrees = temp;

 }

 // must be defined inside a class called farenheit:

 public static explicit operator celsius(fahrenheit f)

 {

   return new celsius((5.0f / 9.0f) * (s - 32));

 }

 public float degrees

 {

   get { return degrees; }

 }

 private float degrees;

}

class mainclass

{

 static void main()

 {

   fahrenheit f = new fahrenheit(100.0f);

   ("{0} fahrenheit", s);

   celsius c = (celsius)f;

   (" = {0} celsius", s);

   fahrenheit f2 = (fahrenheit)c;

   line(" = {0} fahrenheit", s);

 }

}

/*

output:

100 fahrenheit = 37.77778 celsius = 100 fahrenheit

*/

下面的示例定义一个结构 digit,该结构表示单个十进制数字。定义了一个运算符,用于将 byte 转换为 digit,但因为并非所有字节都可以转换为 digit,所以该转换是显式的。

c#

struct digit

{

 byte value;

 public digit(byte value)

 {

   if (value > 9)

   {

     throw new argumentexception();

   }

    = value;

 }

 // define explicit byte-to-digit conversion operator:

 public static explicit operator digit(byte b)

 {

   digit d = new digit(b);

   line("conversion occurred");

   return d;

 }

}

class explicittest

{

 static void main()

 {

   try

   {

     byte b = 3;

     digit d = (digit)b; // explicit conversion

   }

   catch (exception e)

   {

     line("{0} exception caught.", e);

   }

 }

}

/*

output:

conversion occurred

*/

s("content_relate");

【c#转换关键词explicit的使用】相关文章:

1.

c#访问关键词base的使用

2.

c#中预处理指令#if的使用

3.

c#中访问关键词 this 的常用用途

4.

c#查询关键字之into的使用

5.

c#中预处理指令#line的使用

6.

c#运算符关键字is的使用

7.

c#查询关键字之group子句的使用

8.

c# 术语大全

声明:准根文档网所有作品(图片、文字)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系 saizw@outlook.com