Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t隐式地将类型字符串转换为int_C# - Fatal编程技术网

C# Can';t隐式地将类型字符串转换为int

C# Can';t隐式地将类型字符串转换为int,c#,C#,这是我的C代码# 问题在于get属性。当我尝试返回时,它显示它无法将类型字符串转换为int! 我试图寻找问题,但仍然没有解决办法 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DefiningProperties { class BookPrices { private string Name; pri

这是我的C代码# 问题在于get属性。当我尝试返回时,它显示它无法将类型字符串转换为
int
! 我试图寻找问题,但仍然没有解决办法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DefiningProperties
{
    class BookPrices
    {
        private string Name;
        private int MarketPrice;
        private int Sale;
        private string Aouther;

        public int price
        { 
            get
            {
                return MarketPrice * Sale;
            }
            set
            {
                MarketPrice = value;
            }
            }

        public int BookDispay
        {
            get
            {
               strong text return Name + ", " + price + ", " + Aouther;
            }
        }

            public BookPrices(string bName, int bMarketPrice, int bSale, string bAouther)
        {
                Name = bName;
                MarketPrice = bMarketPrice;
                Sale = bSale;
                Aouther = bAouther;
        }
        }

    }
}

您的属性的类型为
int
,但您正试图返回一个
字符串

,只需从

public int BookDispay 
对此

public string BookDispay

问题:您没有私有
字符串
字段
书籍显示

解决方案:您需要创建一个私有字符串字段
bookDisplay
,然后写入公共属性,将值设置并获取到私有
bookDisplay
字段中

试试这个:

class BookPrices
{
    private string bookDisplay; //add this private field.
    public string BookDispay    // now create a public property here
    {
        get
        {
          bookDisplay = Name + ", " + price + ", " + Aouther;
          return bookDisplay ;
        }            
    }

你在财产上的签名是错误的

您将其声明为
Int
,并返回一个
字符串


Int
替换为
String

您不理解错误消息的哪一部分?特别是,你认为
int BookDispay
是什么意思?
price
将被隐式转换。如果引用为null,则与字符串连接运算符“+”一起使用的任何对象将由框架处理,以转换为空字符串,否则转换为obj.ToString()。答案相应地更改:)我刚才使用的.net,但是我记得我必须显式地强制转换,在.net的某个版本中是否发生了这种变化,还是一直如此?什么是
private string bookDisplay
for?@Selman22:编辑了我的帖子。你提到的问题无关紧要。您正在更改要求。这毫无意义。您现在可以设置
bookDisplay
,但是
BookDispay
的getter将永远不会返回它。@BartvanNierop:编辑了我的帖子