这是啥语法

谁学逆向工程 2012-12-31 10:59:16
这咋用,干啥用的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Csharp
{
class People
{
private int myInt;
public int MyIntProp//这是什么玩意
{
get
{
return myInt;
}
set
{
myInt = value;
}
}
}
class Program
{
static void Main(string[] args)
{
People p = new People();
p.MyIntProp = 1;
Console.ReadKey();
}

}
}
...全文
211 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Assassin_ 2012-12-31
  • 打赏
  • 举报
回复
属性
wonkju 2012-12-31
  • 打赏
  • 举报
回复
我也转到 C# 不久,相当于 java 中 的属性的set 和get方法, 在C# 中,规范了属性用大写,字段用小写,如: private int age -> 字段。 public Age{set{this.age = value};get{return this.age}} 这些都是C# 内置的工作,不明白的话,可以看 Reflector ··· 这是平时写的,都省略了! 就如, 对象.Age = "Lisi" ->内部调用 Age{set{this.age = value} int output = 对象.Age ->内部调用 get{return this.age}
showjim 2012-12-31
  • 打赏
  • 举报
回复
还有一种写法
        public int myInt { get; set; }
下面这种写法还有点意义
        public int myInt { get; private set; }
threenewbee 2012-12-31
  • 打赏
  • 举报
回复
引用 5 楼 xiaoyuanyuan2009 的回复:
引用 3 楼 whereisrxy 的回复:目测楼主是学C++的...我也不懂这是啥意思 你真是好眼力。 引用 4 楼 taomanman 的回复:这个是属性语法啊,如果是.NET FrameWork 3.0以上的话,可以简写成…… 这属性是干啥的
按照面向对象的做法,一个对象不应该有任何共有字段。 也就是一个对象只能通过它自己的方法去修改自身状态。 比如 class People { public int Age; } 这是不好的设计 你应该设计成 class Prople { private int age; public int Get_Age() { return age; } public void Set_Age(int value) { age = value; } } 为此,C#提供了简略的写法 class Prople { private int age; public int Age { get { return age; } set { age = value; } } } 也就是 class Prople { public int Age { get; set; } }
showjim 2012-12-31
  • 打赏
  • 举报
回复
相当于
        private int myInt;
        public int Get_myInt()
        {
            return myInt;
        }
        public void Set_myInt(int value)
        {
            myInt = value;
        }
当myInt与接口无关时,其实就是
        public int myInt;
showjim 2012-12-31
  • 打赏
  • 举报
回复
        private int myInt;
        public int MyIntProp
        {
            get
            {
                return myInt;
            }
            set
            {
                myInt = value;
            }
        }
这种写法就是脱裤子放屁。
谁学逆向工程 2012-12-31
  • 打赏
  • 举报
回复
引用 3 楼 whereisrxy 的回复:
目测楼主是学C++的...我也不懂这是啥意思
你真是好眼力。
引用 4 楼 taomanman 的回复:
这个是属性语法啊,如果是.NET FrameWork 3.0以上的话,可以简写成……
这属性是干啥的
暖枫无敌 2012-12-31
  • 打赏
  • 举报
回复
这个是属性语法啊,如果是.NET FrameWork 3.0以上的话,可以简写成 private int myInt; public int MyIntProp { get { return myInt; } set { myInt = value; } } ========》简写成 public int MyIntProp { get;set; }
xiaoyu_code 2012-12-31
  • 打赏
  • 举报
回复
目测楼主是学C++的...我也不懂这是啥意思
threenewbee 2012-12-31
  • 打赏
  • 举报
回复
MyIntProp是My(我的)Integer(整数)Property(属性)的意思。
threenewbee 2012-12-31
  • 打赏
  • 举报
回复
这个是属性啊。

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧