C # Basic Knowledge: C # class and structure (1 ),

Source: Internet
Author: User

C # Basic Knowledge: C # class and structure (1 ),

1. What are the structural features? Implementation code?
The structure is defined by the struct keyword, which is similar to the class but essentially different from the class. The structure is actually a value type and does not need to be allocated.
Structure Features:
(1) When the structure is passed as a parameter, it is a value transfer.
(2) The constructor of the structure must contain parameters.
(3) structure instantiation does not require new.
(4) The structure cannot be inherited, but the interface can be implemented.
(5) The 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>
/// Area
/// </Summary>
Public double CArea ()
{
Return 3.14 * radius;
}
 
}
 
Static void Main (string [] args)
{
Circle circle1; // do not instantiate new
 
Circle1.radius = 5;
 
Console. WriteLine ("circular area:" + circle1.CArea ());
 
Circle circle2 = new Circle (1); // instantiate with new
 
Console. WriteLine ("circular area:" + 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>
/// Area
/// </Summary>
Public double CArea ()
{
Return 3.14 * radius;
}
 
}
 
Static void Main (string [] args)
{
Circle circle1; // do not instantiate new
 
Circle1.radius = 5;
 
Console. WriteLine ("circular area:" + circle1.CArea ());
 
Circle circle2 = new Circle (1); // instantiate with new
 
Console. WriteLine ("circular area:" + circle2.CArea ());
 
Console. ReadLine ();
}
}
}
 
2. What is delegation? Features? When do I use delegation instead of interfaces? How do I declare, instantiate, and use a delegate?
 
(1) A delegate is a class that defines the type of a method and can be used as a parameter of another method. Avoid using branches in programs,
 
Better program scalability.
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 ();
}
}
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 ();
}
} Www.2cto.com
(2) delegate is similar to the C ++ function pointer, but it is type-safe.
 
The delegate allows passing methods as parameters.
 
A delegate can be used to define a callback method.
 
The delegate can be linked together. For example, multiple methods can be called for an event.
 
The method does not need to be exactly matched with the delegate signature. For more information, see covariant and inverter.
 
C #2.0 introduces the concept of anonymous methods, which allow passing code blocks as parameters instead of Individually Defined methods.
 
When using delegation, I think it should be used when different methods need to be called by the branch. For example, in the factory mode, different classes are instantiated according to the branch.
 
In this case, the interface is used.
 
 
A delegate is a class that defines the type of a method so that the method can be passed as a parameter of another method.
 
To avoid using the If-Else (Switch) statement in a large number in the program, and make the program better expandable.
 
Scalability.
 
3. What is a partial/partial category? What features? Implementation code? Applicable scenarios? What rules do I need to follow?
A partial class divides a class into several independent files using the partial keyword, but it is actually a class. Generally, when there are too many lines of a class or some functions are relatively independent, you can use the segmented classes. The most common examples are Form. cs and Form. designer. cs.
Generally, the following rules must be followed:
(1) the partial keyword must be used.
 
(2) Although there are different parts, each part must have the same accessibility, such as public and private.
 
(3) If any part is declared as abstract and sealed, the entire type is considered abstract and sealed.
 
(4) If any part declares to inherit the base class, the entire type will inherit the class
 
(5) different basic interfaces can be specified for each part. The final type will implement all interfaces listed in all segment declarations.
 
(6) Any class, structure, or interface member declared in a division definition can be used by all other parts
 
(7) nested types can be segments, even if the types they are nested in are not segments themselves.

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.