[C #] is and,

Source: Internet
Author: User

[C #] is and,

Let's look at an example:

Public class User {} public class Group {} class Program {static void Main (string [] args) {Object oUser = new User (); Object user = (Group) oUser ;}}View Code


In this case, an error is reported (InvalidCastException). Due to the C # security mechanism, it will judge once. The conversion is invalid here.

 

Is to judge: is to judge whether the object Is the type you want (here, the User)

Class Program {static void Main (string [] args) {Object obj = new User (); if (obj is User) {User user = (User) obj ;}}}View Code

  

In this case, we make a judgment on the obj is User. As we have just said, the compiler will perform an operation like (User) obj (forced conversion, in this way, there is a consumption of performance. Let's take a look at.

 

AS operation: if the object is modified, it is converted. If not, null is returned.

Class Program {static void Main (string [] args) {Object obj = new User (); User user = obj as User; if (user = null) {// handle error //....}}}View Code

 

Note: individuals prefer to use As, which simplifies operations and improves performance. It is convenient to make a NULL judgment and directly handle such exceptions.


[C Language] Is there a function that can clear keys in the cache?

Fflush (stdin)
Clear standard input Cache

# Include "stdio. h"
Main ()
{
Char a, B;
Scanf ("% c", & );
// Fflush (stdin );
Scanf ("% c", & B );
Printf ("\ n % c", a, B );
}

You can try it. If there is no fflush (stdin), if you enter a string of characters "abcd", a = 'A', B = 'B'
If fflush (stdin) exists, after entering "abcd", the program continues to wait for the input, and then enters "efdfsd". The result is a = 'A', B = 'E'

What is the letter in c?

What letter?
What if it's a letter play? [It is a drama for H.
Or c in c? It means cosplay. c is the language cosplay.

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.