在 C# 中定义 get 或 set [英] define get or set in c#

查看:22
本文介绍了在 C# 中定义 get 或 set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我在 C# 类中使用 get 或 set 时遇到问题当我使用 get 或 set 时给出错误(无效令牌 { 在课堂上)请看下面的代码,我有这个问题

friends i have problem with using get or set in class in c# when i use get or set in gives error(invalid token { in class) pls, see below code,i have this problem in it

static int abcd
{  
    get           
    {  
       return  _abcd; 
    }  
}  

谢谢

这是完整的代码,我对你的任何代码都没有这个问题,只是这个:

this is the complete code,i dont have this problem with any of your codes but just this:

namespace ConsoleApplication2
{
   class Program
   {
      class Car
      {
         private int _speed;  
         public int Speed;
         {
              get
              {
                return _speed
              }
          }
       }
   }
}

推荐答案

您发布的代码段很好,也与错误有关,因为它具有正确的 { 编号到 } 并以正确的顺序.

The snippet you have posted is fine as it is, also in regards to the error, as it has the correct number of { to } and in the right order.

查看您放置它的位置(可能在类之外),或者在文件中查找额外的 }.

Look at where you have placed it (possibly outside of a class), or look for extra } in the file.

更新:(以下有问题的编辑)

您的问题在这里:

public int Speed; // <-- ; should not be here

还有:

return _speed // <-- missing the ;

属性应该这样实现:

public int Speed
{
   get
   {
      return _speed;
   }
}

这篇关于在 C# 中定义 get 或 set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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