C # Fundamentals collation: C # classes and structs (1)

Source: Internet
Author: User
1, structural function characteristics? Implementation code?
Structs are defined with struct keywords, similar to classes but intrinsically different. The structure essence is a value type and it does not need to be assigned.
Characteristics of the structure:
(1), when the structure is passed as a parameter, the value is passed.
(2), the structure of the constructor must have parameters.
(3), the structure instantiation can not be new.
(4), structure cannot inherit, but can implement interface.
(5), the instance field cannot be initialized in the struct.
Cases:

using system;using system.collections.generic;using System.text;namespace TestStruct{class            program {public struct Circle//defines a circle {Private Const double PI = 3.1415926;  public double radius;             Radius//<summary>///constructor///</summary> public Circle (double R)            {radius = R;            }//<summary>///area////</summary> public double Carea ()            {return 3.14 * radius * RADIUS;    }} static void Main (string[] args) {Circle circle1;            No new instantiation of Circle1.radius = 5; Console.WriteLine ("Circle Area:" + circle1.            Carea ());  Circle Circle2 = New Circle (1); Instantiate the Console.WriteLine with new ("Circle area:" + circle2.            Carea ());        Console.ReadLine (); }    }}

2. What is a delegate? Characteristics? When do I use delegates instead of interfaces? How do I declare, instantiate, and use a delegate?

(1), a delegate is a class that defines the type of the method, which can be used as a parameter to another method. Avoid using branches in your programs,

Program extensibility is better.
Example:

  Class program    {public        delegate void Printedelegate (string name);        private static void Printeenglish (string name)        {            Console.WriteLine ("Your Name:" + name);        }        private static void Printechinese (string name)        {            Console.WriteLine ("Your Name:" + name);        }        private static void Printe (string name, Printedelegate makegreeting)        {            makegreeting (name);        }        static void Main (string[] args)        {            printe ("Sam Young", printeenglish);            Printe ("Poplar", Printechinese);            Console.ReadLine ();        }    }

(2), a delegate is similar to a C + + function pointer, but it is type-safe.

A delegate allows a method to be passed as a parameter.

Delegates can be used to define callback methods.

Delegates can be chained together; For example, multiple methods can be called on an event.

method does not need to match the delegate signature exactly. For more information, see Covariance and contravariance.

C # version 2.0 introduces the concept of anonymous methods, which allow code blocks to be passed as parameters in place of a separately defined method.

Using delegates, I think it should be used when different methods need to be called by the branch. However, for example in Factory mode, different classes are instantiated based on branching

, then the interface is used.


A delegate is a class that defines the type of the method so that it can be passed as a parameter to another method, which moves the method

The use of If-else (Switch) statements in the program, while allowing the program to have a better

Malleable

3. What is a partial class/partial class? What are the features? Implementation code? Where is the application? How many rules do you need to follow?
A partial class is a class that is divided into several separate files with the partial keyword, but essentially a class. Generally, the form Form.cs and Form.designer.cs are the most common when there are too many rows in a class or some functions can be separated by partial classes.
Generally, the following rules apply:
(1), you must use the partial keyword

(2), although there are different parts, but each part must have the same accessibility, such as public, private, etc.

(3), if any part is declared abstract, sealed, then the entire type is considered abstract, sealed

(4), if any part of the declaration inherits the base class, the entire type will inherit the class

(5), each part can specify a different base interface, the final type will implement all the partial declaration of all the interfaces listed

(6) Any class, struct, or interface member declared in a partial definition can be used by all other parts

(7), nested types can be partial, even if the type in which they are nested is not part of the division itself.

The above is the basic knowledge of C #: C # class and Structure (1) of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.