C# Study Note: Values and References

本文深入探讨了值类型和引用类型的定义与区别,解析了null值与可空类型的概念及使用方法,并介绍了ref和out参数的作用。此外,还详细阐述了装箱与拆箱机制及其对性能的影响。

1.   Reference Type VS Value Type

 

Most primitive types such as int, float, double, char…(NOT string) are value types.

When you declare a value type variable, the compiler generate codes to allocate a block of memory for the variable.

 

Class types are a big part of reference types.

When you declare a class type variable, the compiler does not allocate memory for the actual content. Instead, it allocates a small piece of memory to hold a reference(address) to the actual content.

The memory of the actual content is allocated only when the new keyword is called to create this object.

 

For example:

int i = 42;

int ii = i;

++i;

You can see that i becomes 43 and ii is 42, because i and ii are in different memory space.

Another example:

Circle c = new Circle(42);

Circle rc = c;

rc.setRadius(46);

c.getRadius();

rc.getRadius();

Now you can see the radius of both Circle variable is 46, because both variable refers to the same Circle object in memory.

 

 

2.   null values and Nullable types

 

When you declare a variable of reference type and don’t initialize it, the variable has the value null.

You can also assign null to any variable of reference type. This means the variable doesn’t refer to any class entity.

 

Note: if you explicitly declare a reference variable as null, chances are that the garbage collector will become more actively recycle the resource it used to refer to.

 

The null value is useful for initializing reference types. Sometimes, you need an equivalent value for value types, but null is itself a reference, and so you cannot assign it to a value type. The following statement is therefore illegal in C#:

int k = null; //illegal

However, C# defines a modifier that you can use to declare that a variable is a nullable value type. A nullable value type behaves in a similar manner to the original value type, but you can assign the null value to it. You use the question mark (?) to indicate that a value type is nullable, like this:

int? k = null;   

You can make certain whether a nullable variable contains null by testing it in the same way as a reference type.

if (i==null) {/* …… */}

You can assign an expression of the appropriate value type directly to a nullable variable. The following examples are all legal:

int? j = null;

int k = 99;

j = k;

j = 100;

However, the converse is NOT true. You cannot assign a nullable variable to an ordinary value type variable. So, given the definitions of variables j and k from the preceding example, the following statement is not allowed:

         k=j;

This also means that you cannot use a nullable variable as a parameter to a method that expects an ordinary value type.

         int? m = 1;

    func(m);

 

 

3.   ref and out parameters

 

Generally, when you pass an argument to a method, the corresponding parameter is initialized by a copy of the argument, regardless of whether it is value type or reference type or nullable type.

In some cases, you want to affect the actual argument when calling the method. In this time, a ref parameter is in handy.

void swap(int a, int b)

{

    int temp = a;

    a = b;

    b = temp;

} // this code is of no use

void swap(ref int a, ref int b)

{

    int temp = a;

    a = b;

    b = temp;

} // useful

Remember that C# ensures the rule that a variable must be initialized before it can be used. This rule also applies for parameters, including ref parameters. The code below won’t compile according to this rule.

public static void Main(string[] args)

{

    int a, b;

    swap (a, b);

}

 

However, there might be times when you want the method itself to initialize the parameter. You can do this with the out keyword.

The out keyword is syntactically similar to the ref keyword.

The keyword out is short for output. When you pass an out parameter to a method, the method MUST assign a value to it before it finishes or returns.

Example code:

static void doInitialize(out int param)

{

param = 42;

}

static void Main()

{

int arg; // not initialized

doInitialize(out arg); // legal

Console.WriteLine(arg); // writes 42

}

 

 

4.   Boxing and Unboxing mechanism

 

Boxing means converting value type to reference type, while Unboxing means recover the value type from a reference type.

An example of boxing and unboxing:

int n = 15;

object N = n;

int _n = (int)N; //_n==15

What happens behind the code?

Remember that i is a value type and that it lives on the stack. If the reference inside o referred directly to i, the reference would refer to the stack. However, all references must refer to objects on the heap; creating references to items on the stack could seriously compromise the robustness of the runtime and create a potential security flaw, so it is not allowed. Therefore, the runtime allocates a piece of memory from the heap, copies the value of integer i to this piece of memory, and then refers the object o to this copy.

 

On the other hand, if o does not refer to a boxed int, there is a type mismatch, causing the cast to fail , in which case an InvalidCastException will be thrown. Eg:

Circle c = new Circle(42);

object o = c; // doesn't box because Circle is a reference variable

int i = (int)o; // compiles okay but throws an exception at run time

Keep in mind that boxing and unboxing are expensive operations because of the amount of checking required and the need to allocate additional heap memory. Boxing has its uses, but injudicious use can severely impair the performance of a program.

 

 

5.   Casting data safely: Introducing is and as operators

 

By using a cast, you can specify that, in your opinion, the data referenced by a System.object has a specific type and that it is safe to reference the object by using that type. The C# compiler will not check that this is the case, but the runtime will. If the type of object in memory does not match the cast, the runtime will throw an InvalidCastException.

WrappedInt wi = new WrappedInt();

...

object o = wi;

if (o is WrappedInt)

{

WrappedInt temp = (WrappedInt)o;

// This is safe; o is a WrappedInt

...

}

WrappedInt wi = new WrappedInt();

...

object o = wi;

WrappedInt temp = o as WrappedInt;

if (temp != null)

{

... // Cast was successful

}


The is operator takes two operands: a reference to an object on the left ,and the name of a type on the right. If the type of the object referenced on the heap has the specified type, is evaluates to true; otherwise, is evaluates to false.

The as operator takes an object and a type as its operands. The runtime attempts to cast the object to the specified type. If the cast is successful, the result is returned . If the cast is unsuccessful, the as operator evaluates to the null value and assigns that to temp instead.

 

转载于:https://www.cnblogs.com/roy-mustango/p/4153962.html

本研究基于扩展卡尔曼滤波(EKF)方法,构建了一套用于航天器姿态与轨道协同控制的仿真系统。该系统采用参数化编程设计,具备清晰的逻辑结构和详细的代码注释,便于用户根据具体需求调整参数。所提供的案例数据可直接在MATLAB环境中运行,无需额外预处理步骤,适用于计算机科学、电子信息工程及数学等相关专业学生的课程设计、综合实践或毕业课题。 在航天工程实践中,精确的姿态与轨道控制是保障深空探测、卫星组网及空间设施建设等任务成功实施的基础。扩展卡尔曼滤波作为一种适用于非线性动态系统的状态估计算法,能够有效处理系统模型中的不确定性与测量噪声,因此在航天器耦合控制领域具有重要应用价值。本研究实现的系统通过模块化设计,支持用户针对不同航天器平台或任务场景进行灵活配置,例如卫星轨道维持、飞行器交会对接或地外天体定点着陆等控制问题。 为提升系统的易用性与教学适用性,代码中关键算法步骤均附有说明性注释,有助于用户理解滤波器的初始化、状态预测、观测更新等核心流程。同时,系统兼容多个MATLAB版本(包括2014a、2019b及2024b),可适应不同的软件环境。通过实际操作该仿真系统,学生不仅能够深化对航天动力学与控制理论的认识,还可培养工程编程能力与实际问题分析技能,为后续从事相关技术研究或工程开发奠定基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值