标签: automatic-properties

如何在C#3.0中自定义自动属性

在C#3.0之前,我们这样做:

class SampleClass
{
   private int field;
   public int Property { get { return this.field } set { this.field = value } }
}
Run Code Online (Sandbox Code Playgroud)

现在我们这样做:

class SampleClass
{
   public int Property { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

(看看ma!没有字段!)现在,如果我想自定义Getter或Setter,那么字段必须是C#2.0中的显式字符?

.net properties automatic-properties c#-3.0

2
推荐指数
1
解决办法
270
查看次数

是否在规范中实现了自动属性?

我可以依赖这样一个事实,即名为Foo的属性的基础字段被称为"k__BackingField"吗?

.net c# specifications automatic-properties

2
推荐指数
1
解决办法
426
查看次数

返回接口的自动属性

这就是我今天在编码中看到的好奇心.

以下是示例代码:

public class SomeClass
{
   public IUtils UtilitiesProperty { get; set; }
}

public interface IUtils
{
   void DoSomething();
}

public class Utils : IUtils
{
   void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

编译好了.

什么是UtilitiesProperty?它是一个Util?如果不止一个班级实施了IUTil怎么办?它会在编译失败吗?

c# interface automatic-properties

2
推荐指数
2
解决办法
452
查看次数

为什么我们需要创建类变量来获取和设置属性?

非常简单的问题,但我发现理解我们为什么这么做非常重要.

我可以在类中创建一个属性,如下所示:

第一种方法:

public class MyClass
{
   public string MyProperty {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

第二种方法:

public class MyClass
{
    private string _myProperty;

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }
        set
        {
            _myProperty = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

几乎所有文章都使用后一种方法.为什么我们需要在类中创建一个临时变量来保存字符串值.为什么我们不能只使用第一种方法?第二种方法是否提供任何好处?创建额外的变量来存储值是不是很糟糕的内存和性能?

c# automatic-properties

2
推荐指数
1
解决办法
4248
查看次数

自动实现的获取/设置属性

让C#创建通过使用自动属性创建(即{get; set})生成的私有支持字段是否有任何缺点?

我知道它是自动的,因此您无法自定义get/set,并想知道是否还有其他含义.

谢谢!

.net c# properties accessor automatic-properties

2
推荐指数
1
解决办法
819
查看次数

C#中的自动属性不是导致ovehead吗?

当我有自动属性并尝试从它的类中访问它时,它似乎是一个开销,因为我使用一个函数来访问我的类的成员而不是直接访问它.

如果这是正确的,也许在这种情况下我应该考虑不使用自动属性?

c# overhead automatic-properties

2
推荐指数
1
解决办法
207
查看次数

我可以使用get和set代码创建自动属性(没有私有成员)吗?

在c#中,我可以这样做:

public int Foo { get; set; }
Run Code Online (Sandbox Code Playgroud)

这很好.但是只要我想在getter或setter中做任何事情,我就必须将其更改为:

private int foo;
public int Foo {
    get { return foo; }
    set {
        foo = value;
        DoSomething();  // all that other code, just to add this line!
    }
}
Run Code Online (Sandbox Code Playgroud)

有可能避免这种情况吗?我希望能够做到这样的事情:

public int Foo {
    get;
    set {           
       DoSomething();
    }
}
Run Code Online (Sandbox Code Playgroud)

我有多接近上述?

c# properties automatic-properties

1
推荐指数
1
解决办法
797
查看次数

自动属性问题

目前我正在使用.Net 3.0,但我不知道如何使用自动属性.

例如,如果我想通过Authomatic Properties编写此示例代码,我该怎么办?

private string _name = string.Empty;
private string _family = string.Empty;
//A field with default value
private DateTime _releaseDate = System.DateTime.Now;


//ReadOnly Property
public string Name
{
    get {return _name; }
}

//enforce validation rules on setting 
public string Family
{
    get { _family; }
    set
    {
        if (value.Length < 3)
            return new Exception("Family need at least 3 character long");
        else
            _family = value;
    }
}

// A property from two other fields
public string FullName
{ …
Run Code Online (Sandbox Code Playgroud)

automatic-properties c#-3.0

1
推荐指数
1
解决办法
313
查看次数

你能用自动属性激活事件吗?

我想知道我是否可以使用自动属性,仍然能够触发属性更改的事件.这是我目前的课程.(实际的User类当然有更多的属性/字段).

public delegate void UserEventHandler(object sender, EventArgs e);

public class User
{
    public event UserEventHandler Changed;

    private string _UserName;
    public string UserName
    {
        get
        {
            return _UserName;
        }
        private set
        {
            _UserName = value;
            this.OnChanged(EventArgs.Empty);
        }
    }

    protected void OnChanged(EventArgs e)
    {
        if (Changed != null)
        {
            Changed(this, e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否有一种方法可以利用自动属性并仍然可以触发OnChanged事件.换句话说:半自动属性是否可能?

c# events automatic-properties

1
推荐指数
1
解决办法
2243
查看次数

自动增加字母数字字符php

可以用php自动增加一个字母数字,所以它看起来像:

AB001
AB002
...
...
BA001
...
...
ZZ001
Run Code Online (Sandbox Code Playgroud)

然后需要将此代码添加到mysql中,我在想一个varchar(5).

干杯,

php alphanumeric automatic-properties

1
推荐指数
1
解决办法
5108
查看次数