C # advanced programming-the first bullet in the Notes series-Opening

Source: Internet
Author: User

Although I have done a lot of C # projects before, I have done ASP. net bs architecture, and I have also done winform CS architecture. One day, a colleague showed us the following simple demo:

Public   Class Maintest
{
Static   Void Main ( String [] AGRs)
{
Book book1 =   New Book (); // Defines a book book1
Book book2 =   New Book (); // Defines a book book2
Book1.name =   " C # Advanced Programming " ; // Book1 is called C # Advanced Programming
Book2.name =   " C # Advanced Programming " ; // Book2 is also called C # Advanced Programming
Console. writeline (book1 = Book2 ); // Q: Is boo1 and book2 the same book?
}
};
Class Book
{
Private   String Name =   String . Empty;
Public   String Name
{
Get { Return Name ;}
Set {Name = Value ;}
}
};

What will the above output result be? Some people say that both books are <C # advanced programming>, so they are the same book. Other people have comments again. Although the name is the same, it may not be the same document. My hands are different from yours. In fact, this is a very simple truth, that is, the concept of the value type and the reference type. Classes belong to the reference type. When we create different books, we have created book objects in different places in the memory. When we compare two books, we only use the reference addresses of the two books for comparison. This is the same as the pointer address principle of C ++, so the above comparison result will be false.
Some people have another opinion on why the books with the same name are not equal. Yes, there is no mistake in your thinking. We often need to have this kind of requirement in actual project implementation, that is, the two books are considered to be the same book with the same name, so how should we implement it so that the above running result is true? At this time, we thought of using operator overloading. Therefore, we add the following method to the book class:

Public   Static   Bool   Operator   = (Book B1, book B2)
{
If (B1.name = B2.name)
Return   True ;
Else
Return   False ;
}

Okay, let's compile it, huh? Why? As shown in the following figure, we must define the = overload method! = Overload method:

Public   Static   Bool   Operator   ! = (Book B1, book B2)
{
If (B1.name = B2.name)
Return   False ;
Else
Return   True ;
}

OK, compile, run, and the result is true. In fact, this is a very simple problem, and often some people mistakenly think that the attribute values of the two classes are equal when using it. Due to this small concept, it may cause very serious consequences. This is an error in understanding the value type and reference type. Let's take a look at the following demo:

Static   Void Main ( String [] AGRs)
{
Book book1 =   New Book (); // Defines a book book1
Book book2 =   New Book (); // Defines a book book2
Book1.name =   " C # Advanced Programming " ; // Book1 is called C # Advanced Programming
Book1 = Book2; // Consider the two books as the same book
Book2.name =   " C # getting started " ; // For some reason, the name of book2 has been changed.
Console. writeline (book1.name ); // Q: Is boo1 the original C # advanced programming?
}

I don't need to explain the answer. I spent a lot of effort to explain a very simple reference type problem. Many cool people have their opinions. Well, what I want to express is that even if we are proficient in C #, we can use it to develop many projects, but at the same time, we cannot lose some very basic things. When we skillfully develop more than N projects, do we have to ask ourselves how much we know about C?
I am not a computer student, I have never studied any computer science courses, or even C language courses. My major in college is civil engineering,Programming LanguageI have only learned fortrun. Data structure,AlgorithmI have never been involved in compilation principles, software engineering, and so on. However, with my enthusiasm and infinite interest, I am able to use those programming languages, such as VB, C ++, C # has been done, and projects have been done. How to use these programming languages for project development is definitely not inferior to a computer professional. But can we lose these basic things? The answer is yes, no. I have heard that many programmers are not computer professionals, which also greatly increases my confidence. I will try my best to add the basic things I have missed, from a miscellaneous army to the path of the Orthodox army. Haha ~ Therefore, my <C # advanced programming> Notes series come from here. I hope you will support them more in the future! :)

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.