Types and Variables (C # Learning Notes 02)

Keywords: C#

Types and Variables

[C# Types and Variables (Original Reference to Official Tutorial)] https://docs.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/types-and-variables

C# has two types:

1. Value type

1. Simple Types

  1. Signed integers: sbyte, short, int, long
  2. Unsigned integers: byte, ushort, uint, ulong
  3. Unicode character: char
  4. IEEE Binary Floating Points: float, double
  5. High Precision decimal Floating Points
  6. Bool: bool

2. Enumeration Types

User-defined types in the form enum E {...}
Each enumeration type has a base type that can be an arbitrary integer numeric type.
As with C++, enumerators can use initializers instead of defaults, which are integer values starting from 0.

public class EnumTest
{
    enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

    static void Main()
    {
        int x = (int)Day.Sun;
        int y = (int)Day.Fri;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Fri = {0}", y);
    }
}
/* Output:
   Sun = 0
   Fri = 5
*/

3. Structural types

User-defined types in struct S {...}format
struct is a value type, as long as it is used to encapsulate small data
For example, encapsulate the price, title and author data of the entity "Book" into a structured Book.

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}

4. Value types that can be null

Extensions to all other value types with a value of null
Extensions under standard types are examples of the System.Nullable<T> structure
With an int type as a test, the int type itself cannot be initialized to a null type

int? x = null;
if (x.HasValue)
{
    Console.WriteLine($"x is {x.Value}");
}
else
{
    Console.WriteLine("x does not have a value");
}

Output:

x does not have a value

2. Reference type

1. Class type

  1. All other types of final base classes: object Unicode
  2. String: string
  3. User-defined types in the form of class C {...}

In C #, a class can only be inherited from one base class, but a class can implement multiple interfaces.

inherit Example
nothing class ClassA { }
single class DerivedClass: BaseClass { }
No, implement two interfaces class ImplClass: IFace1, IFace2 { }
No, implement an interface class ImplDerivedClass: BaseClass, IFace1 { }

Class members (including nested classes) can be public, protected internal, protected, internal, private, or private protected. By default, members are private.

public: Any other code in the same assembly or other assembly that references that assembly can access that type or member.

private: Only code in the same class or structure can access that type or member.

protected: Only code in or derived from the same class can access that type or member.

internal: Any code in the same assembly can access that type or member, but not in other assemblies.

protected internal: This type or member can be accessed by any code in the assemblies declared or derived classes in another assembly.

private protected: A type or member can be accessed only within its declared assembly through code in the same class or a type derived from that class.

Note:
Programs compiled into the same dll or exe are in the same assembly, and programs in different dll or exe files are in different assembly. An assembly in. net is a dll or EXE file generated directly by a compiler, including assembly lists, metadata, MSIL, etc. Is a collection of one or more type definitions and resource files.

The following example shows how to declare class fields, constructors, and methods. The example also illustrates how to instantiate objects and print instance data. This example declares two classes. The first class, Child, contains two private fields (name and age), two public constructors, and a common method. The second class, StringTest, is used to include Main.

class Child
{
    private int age;
    private string name;

    // Parametric constructor
    public Child()
    {
        name = "N/A";
    }

    // Constructor with parameters
    public Child(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    // Printing method:
    public void PrintChild()
    {
        Console.WriteLine("{0}, {1} years old.", name, age);
    }
}

class StringTest
{
    static void Main()
    {
        // Create objects by using the new operator:
        Child child1 = new Child("Craig", 11);
        Child child2 = new Child("Sally", 10);

        // Create an object using the default constructor:
        Child child3 = new Child();

        // Display results:
        Console.Write("Child #1: ");
        child1.PrintChild();
        Console.Write("Child #2: ");
        child2.PrintChild();
        Console.Write("Child #3: ");
        child3.PrintChild();
    }
}
/* Output:
    Child #1: Craig, 11 years old.
    Child #2: Sally, 10 years old.
    Child #3: N/A, 0 years old.
*/

2. Interface type

User Defined Types in interface I {...}
Interfaces contain only signatures of methods, attributes, events, or indexers. The class or structure that implements the interface must implement the interface members specified in the interface definition.
Example:

interface ISampleInterface       //Define declarations only
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:(concrete method implementation in class)
    void ISampleInterface.SampleMethod()
    {
        // Method implementation. (Method code)
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

3. Array type

One-dimensional and multi-dimensional, such as int [] and int [,]
Multiple variables of the same type can be stored in an array data structure. Declare an array by specifying the element type of the array.

class TestArraysClass
{
    static void Main()
    {
        // Declare a single-dimensional array. 
        int[] array1 = new int[5];

        // Declare and set array element values.
        int[] array2 = new int[] { 1, 3, 5, 7, 9 };

        // Alternative syntax.
        int[] array3 = { 1, 2, 3, 4, 5, 6 };

        // Declare a two dimensional array.
        int[,] multiDimensionalArray1 = new int[2, 3];

        // Declare and set array element values.
        int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

        // Declare a jagged array.
        int[][] jaggedArray = new int[6][];

        // Set the values of the first array in the jagged array structure.
        jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
    }
}

4. Delegation type

User-defined types in delegate int D(...)
Specific Reference: Events and Delegated Learning Notes 03

https://www.cnblogs.com/asahiLikka/p/11644393.html

Posted by Rictus on Wed, 09 Oct 2019 07:49:15 -0700