Effective C # Principle 6: Distinguishing between value type data and reference type data

Source: Internet
Author: User
Tags shallow copy

Value type data or reference type data? Structure or class? What do you need to use them for? This is not C + +, you can define all types as value types and make a reference to them. This is not Java, all types are value types. When you create each type instance, you have to decide what form they exist in. This is an important decision that must be faced at the outset in order to get the right results. You have to face the consequences of this decision all the time, because if you want to change it later, you have to force a lot of code in many small places. When you design a type, choosing struct or class is a simple little thing, but once your type has changed, there is much more to be done (than at design time) for all users who use that type of user to update.

This is not a simple choice. The right choice depends on how you want your new type to be used. Value types do not have polymorphism, but their access to data in your application is performance-capable, reference types can be polymorphic, and you can define performance behavior for them in your application. Consider what kind of functions you expect to design for your type, and decide what type of design to be based on these functions. Structures store data, and class performance behavior.

Because a lot of common problems exist in C + + and Javaj, so. NET and C # make a distinction between value types and reference types. In C + +, all parameters and return values are passed as value types. Passing with a value type is a very efficient thing to do, but you have to endure the problem: a shallow copy of an object (partial copying) (sometimes called slicing object). If you copy data to a derived object in the form of a base class, only part of the base class's data is copy. You lose all the information of the derived object directly. A virtual function of a base class is used even when.

And the Java language, after abandoning the value type data, more or less some performance. In Javs, all user-defined types are reference types, and all parameters and return data are passed as reference types. This strategy has its advantages in (data) consistency, but it is flawed in performance. Let's face it, some types are not polymorphic-they don't need to be. Java's programming staff prepares a memory heap allocator and a final garbage collector for all variables. They also need to spend extra time on the access to each reference variable because all variables are reference types. In C #, you either declare a value type data using struct, or declare a reference type data with class. Value type data should be relatively small and lightweight. The reference type is inherited from your class. This section will practice using a different method to use a data type so that you can learn the difference between the value type data and the reference type data.

Here we go, there's a type returned from a method:

private MyData _myData;
public MyData Foo()
{
return _myData;
}
// call it:
MyData v = Foo();
TotalSum += v.Value;

If MyData is a value type, then the return value is saved in copy to V. and V is on the stack memory. However, if MyData is a reference type, you have already imported a reference into an internal variable. While

You also violated the encapsulation principle (see Principle 23).

Or, consider this variable:

private MyData _myData;
public MyData Foo()
{
return _myData.Clone( ) as MyData;
}
// call it:
MyData v = Foo();
TotalSum += v.Value;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.