C# Classes and Structures (1)

Keywords: C#

1. Structural and functional characteristics? Implementation code?
Structures are defined by struct keywords, similar to classes, but essentially different. Structure is essentially a value type, and it does not need to be allocated.
Structural characteristics:
(1) When a structure is passed as a parameter, it is a value transfer.
(2) Structural constructors must have parameters.
(3) Structural instantiation can be done without new.
(4) The structure can not be inherited, but the interface can be implemented.
(5) Instance fields cannot be initialized in the structure.
Example:

using System; 
using System.Collections.Generic; 
using System.Text; 
 
namespace TestStruct 
{ 
    class Program 
    { 
        public struct Circle     //Define a circle  
        { 
            private const double pi = 3.1415926; 
 
            public double radius;  //radius  
 
            /// <summary>  
            /// Constructor  
            /// </summary>  
            public Circle(double r) 
            { 
                radius = r; 
            } 
            /// <summary>  
            /// The measure of area  
            /// </summary>  
            public double CArea() 
            { 
                return 3.14 * radius * radius; 
            } 
 
        } 
 
        static void Main(string[] args) 
        { 
            Circle circle1;    //No need new instantiation  
 
            circle1.radius = 5; 
 
            Console.WriteLine("The area of the circle is:" + circle1.CArea()); 
 
            Circle circle2 = new Circle(1);  //use new instantiation  
 
            Console.WriteLine("The area of the circle is:" + circle2.CArea()); 
 
            Console.ReadLine(); 
        } 
    } 
} 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace TestStruct
{
    class Program
    {
        public struct Circle     //Define a circle
        {
            private const double pi = 3.1415926;
 
            public double radius;  //radius
 
            /// <summary>
            /// Constructor
            /// </summary>
            public Circle(double r)
            {
                radius = r;
            }
            /// <summary>
            /// The measure of area
            /// </summary>
            public double CArea()
            {
                return 3.14 * radius * radius;
            }
 
        }
 
        static void Main(string[] args)
        {
            Circle circle1;    //No need new instantiation
 
            circle1.radius = 5;
 
            Console.WriteLine("The area of the circle is:" + circle1.CArea());
 
            Circle circle2 = new Circle(1);  //use new instantiation
 
            Console.WriteLine("The area of the circle is:" + circle2.CArea());
 
            Console.ReadLine();
        }
    }
}

2. What is delegation? Characteristic? When do you use delegates instead of interfaces? How do I declare, instantiate, and use delegations?
 
(1) Delegation is a class that defines the type of method and can be used as a parameter of another method. Avoid using branches in programs.
 
Program scalability 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 tree", PrinteChinese); 
 
          Console.ReadLine(); 
      } 
  } 
  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 tree", PrinteChinese);
 
            Console.ReadLine();
        }
    }

(2) Delegation is similar to C++ function pointer, but it is type-safe.
 
Delegates allow methods to be passed as parameters.
 
Delegates can be used to define callback methods.
 
Delegates can be linked together; for example, multiple methods can be invoked for an event.
 
The method does not need to match the delegated signature accurately. For more information, see Covariance and Inversion.
 
Version C# 2.0 introduces the concept of anonymous methods, which allow code blocks to be passed as parameters instead of individually defined methods.
 
With delegation, I think it should be used when different methods need to be called by branches. But in factory mode, for example, different classes are instantiated according to branches
 
At this point, the interface is used.
 
 
A delegate is a class that defines the type of method so that it can be passed as a parameter of another method, which moves the method.
 
By assigning parameters in a state, If-Else(Switch) statements can be avoided and the program can be expanded better.
 
Malleability.
 
3. What are partial classes/partial classes? What functional features? Implementation code? Applicable occasion? How many rules should be followed?
Partitioned classes are classes that use partial keywords and are divided into several separate files, but in essence they are classes. Generally, when a class has too many rows or some functions are relatively independent, it can be divided into classes. The most common are forms Form.cs and Form.designer.cs.
Generally, the following rules should be followed:
(1) partial keywords must be used
 
(2) Although there are different parts, each part must have the same accessibility, such as public, private, etc.
 
(3) If any part is declared abstract and sealed, the whole type is considered abstract and sealed.
 
(4) If any part declares inheritance of a base class, the entire type will inherit that class.
 
(5) Each part can specify a different base interface, and the final type will implement all the interfaces listed in all part declarations.
 
(6) Any class, structure, or interface member declared in the definition of one part is available to all other parts
 
(7) Nested types can be partitioned, even if the type they are nested in is not partitioned by itself.

Posted by detalab on Mon, 03 Jun 2019 17:33:24 -0700