C#note 02: Namespaces, Classes, Structs, Constants, Operator overloading, if and switch

本文介绍了 C# 编程语言中的基本概念,包括命名空间的使用、类与对象的区别、静态成员与实例成员的不同之处、构造函数与析构函数的作用、结构体的特点以及常量的定义。

Namespaces 

Namespaces are a way of organizing the various types that occur in a c# program. It is somewhat similar in concept to a folder in a computer file system. A C# program contains one or more namespaces and each namespace is either defined by you the programmer, or defined as part of a previously written class library.

For example, the namespace System includes the Console class, a class that contains methods for reading and writing to the console window. The System namespace also contains a number of other namespaces, such as System.IO and System.Collections. The .NET Framework alone has more than eighty namespaces, each with up to thousands of classes: namespaces are used to minimize the confusion that might be caused between similarly named types and methods.

 

Creating Your Own Namespace

It is common to use namespaces when working on large programs. Using your own namespzces provides a degree of control over similarly named methods and types. 

 

Classes 

C# is an object-oriented programming language, and in common with other modern languages, it groups related fields, methods, properties, and events into data structures called classes.

Classes vs. Objects

A class is basically a blueprint for a custom data type. After you define a class, you can create its objects or instances. You create an instance of a class by using the C# keywork new.

Static vs. Instance Members

A static member is a method or field that can be accessed without reference to a particular instance of a class. The most common static method is Main, which is the entry point for all C# programs; note that you do not need to create an instance of the containing class in order to call the Main method. Another commonly used static method is WriteLine in the Console class. Note the difference in syntax when accessing static methods; you use the class name, not the instance name, on the left side of the dot operator: Console.WriteLine.

When you declare a class field static, all instances of that class will "share" that field.

A static class is one whose members are all static. Static classes, methods and fields are useful in certain scenarios for performance and efficiency reasons. However, subtle errors can arise if you assume that a field is an instance field when in fact it is static. 

When designing your program, it is good practice, but not a requirement, to keep a single class in each source code file.

 

Constructors 

Constructors typically set the initial values of variables defined in the class. This is not necessary if the nitial values are to be zero for numberic data types, false for Boolean types, or null for reference types, as these data types are initialized automatically.

You can define constructors with any number of parameters. Constructors that do not have parameters are called default constructors.

 

Destructors 

Because of the automatic garbage collection system in C#, it is not likely that you will ever have to implement a destructor unless your class uses unmanaged resources. 

 

Structs 

A struct is a kind of type that is similar to a class in many ways except that it does not support inheritance. 

 

Structs 

A struct in C# is similar to a class, but it is intended for smaller data structures. A struct is a value type, whereas a class is a reference type. Each struct object contains its own copy of the data, whereas a class object contains only a reference to the data.

A struct types inherit from the Object class, which is a root clas in c#. You cannot inherit from a struct, although a struct can implement interfacws.

A struct typically can be created faster than a class. If in your application new data structures are being created in large numbers, you should consider using a struct instead of a class. Structs are also used to encapsulate groups of data fields such as the coordinates of a point on a grid, or the dimensions of a rectangle. 

 

Constants

A constant is a type of field. It holds a value that is assigned when the program is compiled, and never changes after that. 

A readonly variable is like a constant but its value is assigned when a program starts up. This allows you to set the value based on some other condition that you don't know until the program is running. But after that first assignment, the value cannot change again while the program is running.

 

Operator Overloading

C# supports operator overloading; this allow you to redefine operators to be more meaningful when used with your own data types. 

 

If and Switch

Unlike C and C++, if statements require Boolean values. For example, it is not permissible to have a statement that doesn't get evaluated to a simple True or false, such as (a=10). In C#, 0 cannot be substituted for False and 1, or any other value, for True.

The expression that the switch statement uses to determine the case to execute must use the Built-in Data Types, such as int or string; you cannot use more complex user-defined types.

内容概要:本文介绍了一个基于Matlab的综合能源系统优化调度仿真资源,重点实现了含光热电站、有机朗肯循环(ORC)和电含光热电站、有机有机朗肯循环、P2G的综合能源优化调度(Matlab代码实现)转气(P2G)技术的冷、热、电多能互补系统的优化调度模型。该模型充分考虑多种能源形式的协同转换与利用,通过Matlab代码构建系统架构、设定约束条件并求解优化目标,旨在提升综合能源系统的运行效率与经济性,同时兼顾灵活性供需不确定性下的储能优化配置问题。文中还提到了相关仿真技术支持,如YALMIP工具包的应用,适用于复杂能源系统的建模与求解。; 适合人群:具备一定Matlab编程基础和能源系统背景知识的科研人员、研究生及工程技术人员,尤其适合从事综合能源系统、可再生能源利用、电力系统优化等方向的研究者。; 使用场景及目标:①研究含光热、ORC和P2G的多能系统协调调度机制;②开展考虑不确定性的储能优化配置与经济调度仿真;③学习Matlab在能源系统优化中的建模与求解方法,复现高水平论文(如EI期刊)中的算法案例。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码和案例文件,按照目录顺序逐步学习,重点关注模型构建逻辑、约束设置与求解器调用方式,并通过修改参数进行仿真实验,加深对综合能源系统优化调度的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值