当前位置:首页 > .NET > C#

C# and Game(二)Classes Structs Delegates Enum Events

优良自学吧提供C# and Game(二)Classes Structs Delegates Enum Events,C# and Game(2)Classes Structs Delegates Enum Events C# and Game(2)Classes Structs Delegates Enum EventsClass and Struct

C# and Game(2)Classes Structs Delegates Enum Events
C# and Game(2)Classes Structs Delegates Enum Events

Class and Structs
Class is a reference type. Person p, p is instance, Person is class/type. Multiple instances of the same Person type can be created, and each instance can have different values in its properties and fields.

A struct is a value type. When the struct is assigned to a new variable, it is copied, the new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

There is no global variables or methods as in other languages. Everything will be define within a class or struct.
List of properties of Class: Fields, Constants, Properties, Methods, Constructors, Destructors, Events, Indexers, Operators, Nested Types.

Method Access - public, protected, internal, protected internal, private. The default is private.

Classes (but not structs) support the concept of inheritance.

Classes and structs can inherit multiple interfaces.

Classes (but not structs) can be declared as static. A static class can contain only static members.

Classes
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/classes

A class is a construct that enables you to create custom types by grouping together variables, methods and events.
We can use class by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. (JAVA?)

Customer object3 = new Customer();
Customer object4 = object3;
All changes made to object3 will affect object4 as well. Class is a reference type.

Class Inheritance
public class Manager : Employee {}

Objects - Instances
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/objects
A class definition is like a blueprint. An object is basically a block of memory that has been allocated and configured according to the blueprint.

Structs
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/structs
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/using-structs
public struct PostalAddress{
    //Fields, properties, methods and events
}

Inheritance and Polymorphism
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/polymorphism

Static Classes and Static Class Members
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

Constants
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/constants
class Calendar1
{
    public const int months = 12;
}

How to Declare and Use Read Write Properties
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/how-to-declare-and-use-read-write-properties
class Person{
    private string name = “”;

    public string Name{
        get{
            return name;
        }
        set{
            name = value;
        }
    }
}

Auto-Implemented Properties
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/auto-implemented-properties
class Customer{
    public string Name { get; set; }
}

Finalizers
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/destructors
class Car{
    ~Car(){
        //destructor - cleanup statements...
    }
}

Delegates
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/delegates/
delegate is a type that safely encapsulates a method, similar to function pointer in C++.
#1 Example
//create a method for delegate
public static void DelegateMethod(string message){
    System.Console.WriteLine(message);
}

Del handler = DelegateMethod;
//call the delegate
handler(“Hello");

public void MethodWithCallback(int param1, int param2, Del callback){
    callback(“The number is = “ + (param1 + param2).ToString());
}

MethodWithCallback(1, 2, handler);

#2 Example
MethodClass obj = new MethodClass();
Del d1 = obj.Method1;
Del d2 = obj.Method2;
Del d3 = DelegateMethod;

Del allMethodsDelegate = d1 + d2;
allMethodsDelegate += d3;

Enumeration Types
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/enumeration-types
enum MachineState{
    PowerOff = 0,
    Running = 5,
    Sleeping = 10,
    Hibernating = Sleeping + 5;
}

Events
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/events/
Events enable a class or object to notify other classes or objects.
Here is a example there
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines





References:
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/delegates/
main index
https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/index







(本文来自互联网,不代表搜站(http://www.ylzx8.cn/)的观点和立场)
本站所有内容来自互联网,若本站收录的信息无意侵犯了贵司版权,请给我们来信(ylzx8cn@163.com),我们会及时处理和回复,谢谢