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

本文详细介绍了C#中的类(Class)与结构体(Struct)的概念与使用方法,包括字段、属性、方法等组成部分,并解释了引用类型与值类型的区别。此外还涉及继承、多态、委托、枚举及事件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值