Re-reading C # the application of several operators in advanced programming: Is, As, sizeof, typeof, GetType ,??

Source: Internet
Author: User
These symbols have been used more or less. Today we will summarize them based on the proc # statement:
Is:
Check whether the variable type matches the specified type. True or false is returned. No error is reported.
Honestly, I have never used it. Take a look at the following instance Code , It is easy to understand:   Int I =   100 ;

If (I Is   Object ) // True or false
{
Response. Write ("I is object </BR>");
}

However, it is more often used to determine whether an unknown type (object) matches the specified type. Static   Void Test ( Object O)
{
Class1;
Class2 B;

If (O Is Class1)
{
Console. writeline ("O is class1");
A=(Class1) O;
//Do something with "."
}
}

At this time, I often use as instead.
As:
Type conversion. If the conversion fails, null is returned and no error is reported. Object O =   " Hi " ;
String S2 = O As   String ;
If (S2 ! =   Null )
{
Response. Write ("OK </BR>");
}

In actual development, as is often used. When an object is obtained, it is not known its type. It can be used only after the conversion is successful, this is somewhat similar to is.
Application 1: Dataset DS =   New Dataset ();
// Set values to DS here
Session [ " Data " ] = DS;
Dataset DS2 = Session [ " Data " ] As Dataset;
If (DS2 ! =   Null )
{
//Code here
}

Application 2: Button BTN = Form1.findcontrol ( " BTN " ) As Buttonl;
// Note: Normally, here is the gridview or others data show contorls
If (BTN ! =   Null )
{
//Code here
}

At this time, using is can also achieve the goal. Dataset DS =   New Dataset ();
// Set values to DS here
Session [ " Data " ] = DS;
If (Session [ " Data " ] Is Dataset)
{
Response. Write ("OK");
}

Null type:
For example, int cannot be null, but if it is identified as follows:Int?J= Null;
Console. writeline (j );

?? : Combined with the symbol that can be empty, format: ?? B; if expression A is null, the value of B is returned. Otherwise, the value of a is returned.
Note that a and B must have an empty type:

Int I =   22 ;
Int M =   23 ;
Int ? N =   12 ;
// Console. writeline (I ?? M ); // Error
Console. writeline (J ?? M ); // Output 23
Console. writeline (n ?? M ); // OUTPUT 12

Sizeof: Used for the size of the return value type in memory. Note that it can only be a value type and cannot be a reference type:

Console. writeline ( Sizeof ( Byte )); // Output 1
Console. writeline ( Sizeof ( Int )); // Output 4
Console. writeline ( Sizeof ( Long )); // Output 8

Typeof: Get typeSystem. Type.
GetType (): If you want to obtain the type of the object at runtime, you can use this method.
Application:

Foreach (Control CTL In CTLs. Controls)
{
If (CTL. GetType () =   Typeof (Textbox ))
{
Textbox C=CTLAsTextbox;
C. Text= "";
}
}

Typeof is also useful in reflection, and then demo when learning reflection.

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.