Beginner with c #6

Source: Internet
Author: User

1. 6. Unified system Type)
C # A unique type-a unified system type (I have a headache for this death. Make sure you have a better name.
Me ). In short, all other types, including values and references, can be treated as a unified system type. Conceptually,
All types are derived from it. In this way, attributes and methods of the unified system type can be used for other types. Including some
Single "type, such as: int. Let's give an example :*/
Using System;
Class Test
{
Static void Main (){
Console. WriteLine (3. ToString ());
}
}
/* "3. ToString ()" calls the "ToString ()" method of the object. I believe that all the friends who have learned c/c ++ know that they want to output
It's time to worry about numbers. Another one :*/
Class Test
{
Static void Main (){
Int I = 123;
Object o = I; // boxing
Int j = (int) o; // unboxing
}
}
/* In this example like a hat trick, convert from "int" to "object" and then back. In this way
A bridge is built between them. How can this be used. An impromptu example is like min. In c/c ++ :*/
// C/c ++ code

Void min (int I, int j)
{
Return (I <j )? I: j );
}

/* What if the comparison is not int, or maybe int, float, or double? You can do this :*/

Template <class T>
T min (T I, T j)
{
Return (I <j )? I: j)
}

/* Use c :*/
Void swap (object a, object B)
{
Return (I <j )? I: j );
}

/* I think you must see that if the second example compares an int and a float, some conversions are required, while the third one is
In this example, we can compare all the variables! This flexibility is too great. Therefore, I personally think that everyone must be careful when using it!
It will never report an error when comparing an int with a class. Haha, I found that my translation is always getting farther and farther, always
Cannot hold the original text. You have been tampered with a lot. Please forgive me!

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.