Csharp+asp.net Series (iv) (2)

Source: Internet
Author: User
Tags arrays integer reference
Asp.net| Tutorial 9. [] Operator
The brackets ([]) are used for arrays, indexers, and properties, and can also be used for pointers.
Type [] array [indexexpr]
Where: type. Array of arrays. Indexexpr an index expression
10. () operator
In addition to specifying the order of operators in an expression, parentheses are used to specify conversions (type conversions)
(type) expr where: type expr is to be converted to. Expr an expression. Conversions explicitly call conversion operators from expr types to type types, and if no such conversion operator is defined, the conversion fails.
12. Self-added self-subtraction operator
The increment operator + + adds 1 to the value of the variable, and the decrement operator--minus 1 for the value of the variable. This operator has a prefix. For the prefix operator, follow the principle of "first add or subtract, then use", the suffix operator is just the opposite, is "first use, after the increase or decrease"
Using System;
Class Mikecat
{
public static void Main ()
{
Double x,y;
x=1.5;
Console.WriteLine (++x);//self-increasing is equal to 2.5.
y=1.5;
Console.WriteLine (y++)//first show 1.5 after self increase
Console.WriteLine (y);//self-increment equals 2.5
}
}
13.as operator
The as operator is used to perform conversions between compatible types. The as operator is used in an expression of the following form: expression as type where: expression An expression of a reference type. Type reference types.
The as operator is similar to a type conversion, but when the conversion fails, the AS operator produces null instead of throwing an exception. In form, the expression of this type:
The expression as type is equivalent to:
Expression is type? (type) expression: (type) NULL
Only expression are counted once.
Note that the AS operator only performs reference conversions and boxing conversions. The as operator cannot perform other transformations, such as user-defined conversions, which should use cast expressions instead of their execution.
Using System;
Class MyClass1
{
}
Class MyClass2
{
}
public class Istest
{
public static void Main ()
{
Object [] myobjects = new Object[6];
Myobjects[0] = new MyClass1 ();
MYOBJECTS[1] = new MyClass2 ();
MYOBJECTS[2] = "Hello";
MYOBJECTS[3] = 123;
MYOBJECTS[4] = 123.4;
MYOBJECTS[5] = null;
for (int i=0; i<myobjects.length; ++i)
{
string s = myobjects[i] As String;
Console.Write ("{0}:", i);
if (s!= null)
Console.WriteLine ("" "+ S +" ");
Else
Console.WriteLine ("Not a string");
}
}
}
Output
0:not a string
1:not a string
2: ' Hello '
3:not a string
4:not a string
5:not a string
14.new operator
The new operator is used to create a novel type instance with three different forms:
A: Object creation expression, used to create an instance of a class type or a value type.
B: An array creation expression used to create an instance of an array type.
C: Delegate creation expression, used to create a new instance of a delegate type.
15.typeof operator
The typeof operator is used to obtain the type of the system prototype object.
Using System;
Class Mikecat
{
public static void Main ()
{
Console.WriteLine (typeof (int));
Console.WriteLine (typeof (System.Int32));
}
}//results: System.Int32 System.Int32
indicates that int and System.Int32 are the same type
Use the GetType () method in C # to get the type of an expression at run time
Using System;
Class Mikecat
{
public static void Main ()
{
int r=3;
Console.WriteLine ("The area of the circle equals {0}", R*r*math.pi);
Console.WriteLine ("Type is {0}", (R*r*math.pi). GetType ());
}
The area of the}//circle equals 28.2743338823081.
Type is System.Double
16.sizeof operator
The sizeof operator gets the byte size of a value type
Using System;
Class Mikecat
{
Unsafe public static void Sizesof ()
{
Console.WriteLine ("Short size is {0}", sizeof (short));
Console.WriteLine ("Size of int is {0}", sizeof (int));
Console.WriteLine ("The size of long is {0}", sizeof (long));
}
public static void Main ()
{
Sizesof ();
}
The size of the}//short is the size of the 2;int is the size of the 4;long is 8;
17.checked and unchecked operators
An overflow can occur when an integer arithmetic operation is performed or when an integer display is converted to another integral type.
Check this overflow C # has two ways of handling:
First: Set overflow check option at compile time (the overflow checksum is disabled by default):
csc/checked Test.cs//This we have introduced in the previous
Second: Use the checked and unchecked operators to determine whether overflow checksums are performed. If overflow checksums are disabled at compile time, the calculation also throws an exception.
Using System;
Class Mikecat
{
public static void Main (string[] args)
{
Long factorial=1;
Long Num=int64.parse (args[0]);
for (long cur=1;cur<=num;cur++)
{
Checked{factorial*=cur;}
}
Console.WriteLine (factorial of {0} is {1} ", num,factorial);
}
The factorial of}//test.exe 3 3 is 6.
The unchecked operator is just the opposite of the checked operator, and the code enclosed by the unchecked operator does not throw an exception even if it overflows.
Precedence of individual operators I'm not here to say more. The main is hand tired. Oh. remains similar to previous C + + precedence. Refer to MSDN for details. Thank you for your attention to this tutorial, welcome to visit the old cat's ideal blog.

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.