• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ProfessionalC#学习笔记(三c#对象和类型)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

要点摘要:

  

The Differences  between classes and structs

  • classes are reference types stored in the heap ; structs are value types ,they are stored either in the stack or inline and have the same lifetime restrictions as the simple data types.
  •  structs do not support inheritance
  • there are some differents in the way constructors work for structs .In particular,the compiler always supplies a default no-paramater constructors,which you are not permittedto replace.
  • With a struct ,you can specify how the fields are be laid out in memory.

Properties

  1) read-only and write-only properties:

       create a read-only property by simply omitting the set  accessor from the property definition;

      create a write-only property by omitting the get accessor.

  2)access modifers for properties:

      e.g. (get is public and set is private) 

    public string Name
    {
        get
        {
            return _name;
        }
        private set 
        {
            _name = value;
        }
    }

3) auto implimented properties
public string ForeName {get; set;}
public string ForeName {get;} 
public string ForeName {get; private set;}

 

Static Constructors

a static no - parameter constructor for a class will be executed only once, as opposed to the constructors written so far, which are
instance constructors that are executed whenever an object of that class is created.

e.g.
 public MyClass

{

  static MyClass()

{

//initialization code

}

//rest of the class definition

}

Notice:

i) the static constructor does not have any access modifiers. It ’ s never called by any other C# code, but always by the .NET runtime when the class is loaded

ii) the static constructor can never take any parameters, and there can be only one static constructor for a class.

iii) a static constructor can access only static members, not instance members, of the class


ReadOnly Fields

constants don ’ t necessarily meet all requirements.On occasion, you may have some variable whose value shouldn ’ t be changed, but where the value is not known until runtime. C# provides another type of variable that is useful in this scenario: the readonly field

The rule is that you can assign values to a readonly field inside a constructor, but not anywhere else. It ’ s also possible for a readonly field to be an instance rather than a static field, having a different value for each instance of a class.This means that, unlike a const field, if you want a readonly field to be static, you have to declare it as such.

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#非常重要基础之多态发布时间:2022-07-13
下一篇:
c# 那些不常用的操作发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap