C #-Thoughts on automatic attributes,

Source: Internet
Author: User

C #-Thoughts on automatic attributes,
What are automatic attributes?

The automatic attribute is to write only one attribute at the time of writing, without writing segments. It is generally not implemented.

Why use automatic attributes?

The following program 1 tests the use of automatic attributes in general classes. It is found that the compiler will automatically generate a private field when generating a solution, this can be seen in some decompilation software. The purpose of using automatic attributes in a class is that you don't need to write segments, but the problem arises again. Since there is no implementation in the automatic attributes, why not use a public field directly, do I save both set, get and pair of curly braces? I thought about the reasons for programming habits. People are used to calling upper-case attributes, instead of lower-case fields, and using attributes to protect fields. Why not use upper-case fields, it is also a problem of programming habits. The recommended fields are written in lower case, and there is an underline in front. The programming habits are easy to understand.

What if I implement automatic fields?

It is better to use questions to drive us to learn. How can we achieve this? Will it explode? I tried to implement the automatic attribute Age of program 1 and found that the get and set methods must be implemented at the same time. If only one of them is implemented, the compilation will fail, I guess this attribute is not an attribute after the set and get methods are implemented. It can only be a code segment. There are two methods in the code segment, because the field is not automatically generated after the implementation of the decompilation. If only one other is implemented, the system will prompt that the class is not an abstract class or something. If it is not an abstract class, the method cannot be implemented without generating fields. This indicates that the compiler considers this code block as a code block.

So how can we use automatic attributes.

There are two application scenarios

1. defined in the interface, the class that inherits the interface can be used.
2. When you have a field that does not require restricted access, you do not want to write it as a public field for programming habits, and you are too lazy to write that field name, use automatic attributes.

How to use automatic attributes for interfaces

In the above application scenario, the interface can declare automatic attributes. After the declaration, it means that all non-Abstract subclasses of the inherited interface must implement this automatic attribute, and the abstract subclass does not need to be implemented. So I don't know what the name of the generated field is. How can I implement it? The answer is that there is no way to implement it. I can only copy it according to the method of the interface, in this way, automatic attributes are useless in the interface. The only user is to force non-Abstract subclass to have this attribute. Why. In program 2, it is found that compilation fails if it is not implemented. It is concluded that automatic attributes should not be used in interfaces. Then, the only function of automatic attributes is to become lazy while complying with programming specifications.

Other syntax

Access modifiers cannot be added to interface methods. The default value is public (the class is private by default) and cannot be implemented (different from the abstract class). override (different from the abstract class) is not required for subclass implementation ), cannot Write segments; can write automatic attributes (there is no such thing, or it may be that my knowledge is too thin)

Procedure 1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _31_interface{    class Program    {        static void Main(string[] args)        {            Person p = new Person();            p.Name = "leifeng";            p.Age = 10;            Console.WriteLine("name is {0},age is {1}",p.Name,p.Age);            Console.ReadKey();        }    }    public class Person    {        public string Name        {            set;            get;// { return "SB"; }        }        //Cannot write in this way,because this will cause dead ciculation;in Set and Get,we can not use auto properity;        //so we would better not to fulfill the set and get method of properity;        //so if we need not to limit the get and set method,we can use the auto properity, but why not use the public field;        //So the conclusion is we can only use the auto properity in Interface;        //what interests me is that we can not just fulfill one of the tow method;        public int Age        {            set;            get; { return 20; }        }        private char _gender;        public char Gender        {            get { return _gender; }            set { _gender = value; }        }    }}
Procedure 2:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _31_interface{    public interface IRunable    {         int NumOfLegs        {            set;            get;        }         void Fly();    }    public class Car:IRunable    {        public int NumOfLegs        {            get;            set;        }        public void Fly()        {            Console.WriteLine("Car can run on the road!");        }    }    class Program    {        static void Main(string[] args)        {            Console.ReadKey();        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.