Limitations of some methods in C #

Source: Internet
Author: User
Tags sendfile

1. Some methods in C # are always private?

If you are the designer of a class, design a partial class such as MyClass. When you have finished designing this class, others can use it.

For the consumer of a class, the appearance of a class is a property, field, and method of various public.

If you define a partial method sendfile to transfer files in MyClass, you do not provide the implementation code for this method.

Now the class user knows that there is a public method in the MyClass class Sendfile can be used to transfer files, he does not know if this method is part of the method.

He thinks this method is sure to work, and you don't have to provide implementation code!

People will think that the file is normal transmission, but in fact he was "deceived" by you.

So, in order to cause this kind of tragedy to happen, let some methods be defined as private.

As a result, even if this deception happens again, it is self-deception and will not deceive the user of the class.

The customer is God.


2. The parameters of some methods in C # must be ref parameters, not out parameters?

We can think of the method in the class as a machine, and we can use this machine to do something for me.

For out parameters, it's like we're saying to this machine, "Hey, help me produce something that I need to use next."

Note that the out parameter can be unassigned before it is used, but only declared.

A variable that is declared but not assigned will not exist in memory.

This variable is only pushed into the stack when it is assigned.

This means that you are firm, that the machine must be there, that it will work, that it will help you produce what you want, and then you will be assured of using the machine to produce something.

The result is tragic, you're using part of the method, and this part of the method is not implemented.

In other words, you use a machine that doesn't exist at all, try to produce something that doesn't exist, and you use it again and again.

In code, you are manipulating a variable that does not exist at all.

Is that okay? Of course not.

However, if this parameter is ref, there is no problem, if it is ref, then the first means that the variable is present.

Still regard the method as a machine.

Now you're letting the machine help you with the process, not the production.

Even if this machine does not help you to process it is OK, I this item still exists, when I continue to use this item below, no longer "operate a non-existent item" this tragedy.


3. Some methods cannot use virtual, abstract, override, new, sealed, and extern modifiers

Take virtual for a while.

If a partial method is virtual, it means that the derived class can override this part of the method.

To override a method, use the keyword override

As a result, you did not implement this part of the method

Then when the compiler compiles the program, the method is removed because the method body of the partial method is not found.

means that a virtual method does not exist in the base class

You also use the Override keyword in the base class to rewrite the method that has been deleted.

Certainly won't let you through compiling.

The others are similar.


4. Why some methods cannot have return values

The reason and the out parameters are the same, and you expect the machine to produce something for you, but not, and you then use something that doesn't exist.


5. Summary:

Why do you want to set so many conditions for some methods?

A word summed up: fear that you have used there is no thing!!!

Limitations of some methods in C #

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.