Sei sulla pagina 1di 6

false je-r7jYy7vMv0NrJ

/w EPDw UBMGRk

C# Language Reference

MSDN Library Collapse All

C# Operators
C# provides a large set of operators, which are symbols that specify which operations to perform in an expression. Operations on integral types such as ==, !=, <, >, <=, >=, binary +, binary -, ^, &, |, ~, ++, --, and sizeof() are generally allowed on enumerations. In addition, many operators can be overloaded by the user, thus changing their meaning when applied to a user-defined type. The following table lists the C# operators grouped in order of precedence. Operators within each group have equal precedence.

Operator category

Operators

Primary

x.y f(x) a[x] x++ x-new typeof checked unchecked ->

Unary

+ ! ~

++x --x (T)x true false & sizeof

Multiplicative

* / %

Additive

+ -

Shift

<< >>

Relational and type testing

< > <= >= is as

Equality

== !=

Logical AND

&

Logical XOR

Logical OR

Conditional AND

&&

Conditional OR

||

Conditional

?:

Assignment

= += -= *= /= %= &= |= ^= <<= >>= ??

Arithmetic Overflow

The arithmetic operators (+, -, *, /) can produce results that are outside the range of possible values for the numeric type involved. You should refer to the section on a particular operator for details, but in general:

Integer arithmetic overflow either throws an OverflowException or discards the most significant bits of the result. Integer division by zero always throws a DivideByZeroException.

Floating-point arithmetic overflow or division by zero never throws an exception, because floating-point types are based on IEEE 754 and so have provisions for representing infinity and NaN (Not a Number).

Decimal arithmetic overflow always throws an OverflowException. Decimal division by zero always throws a DivideByZeroException.

When integer overflow occurs, what happens depends on the execution context, which can be checked or unchecked. In a checked context, an OverflowException is thrown. In an unchecked context, the most significant bits of the result are discarded and execution continues. Thus, C# gives you the choice of handling or ignoring overflow. In addition to the arithmetic operators, integral-type to integral-type casts can cause overflow, for example, casting a long to an int, and are subject to checked or unchecked execution. However, bitwise operators and shift operators never cause overflow.
See Also Tasks Operator Overloading Sample Reference Overloadable Operators (C# Programming Guide) C# Keywords Concepts C# Programming Guide Other Resources C# Reference Visual C#

Navigation
MSDN Library Design ToolsDevelopment Tools and LanguagesMobile and Embedded Development.NET DevelopmentOffice DevelopmentOnline ServicesOpen Specificationspatterns & practicesServers and Enterprise DevelopmentSpeech TechnologiesWeb DevelopmentWindows Development Development Tools and Languages Visual Studio 11 Developer PreviewVisual Studio 2010Visual Studio 2008Visual Studio 2005Visual Studio .NETWindows Phone DevelopmentSQL Server Developer Tools, code-na...XNA Game StudioMicrosoft Solver Foundation 3.1Visual Studio 6.0Visual FoxProMicrosoft Help SystemMicrosoft RoboticsWindows SDK Visual Studio 2005 Visual StudioVisual SourceSafeVisual Studio Team SystemTechnical ArticlesVisual Studio ExtensibilitySoftware Licensing and Protection S... Visual Studio Introducing Visual StudioIntegrated Development Environment ...Deciding Which Technologies and Too...Windowsbased Applications, Compone....NET Framework Programming in Visua...Visual BasicVisual Basic Power PacksVisual C#Visual C++Visual J#JScriptVisual Web DeveloperVisual Studio Tools for OfficeSmart Device DevelopmentTools and FeaturesVisual Studio SDKGlossary (.NET Framework)

Visual C# Getting Started with Visual C#Using the Visual C# IDEMigrating to Visual C#Writing Applications with Visual C#C# Programming GuideC# ReferenceVisual C# Samples C# Reference C# KeywordsC# OperatorsC# Preprocessor DirectivesC# Compiler OptionsC# TerminologyC# Language Specification C# Operators [] Operator() Operator. Operator:: Operator+ Operator- Operator* Operator/ Operator% Operator& Operator| Operator^ Operator! Operator~ Operator= Operator&lt; Operator&gt; Operator?: Operator++ Operator-Operator&& Operator|| Operator&lt;&lt; Operator&gt;&gt; Operator== Operator!= Operator&lt;= Operator&gt;= Operator+= Operator-= Operator*= Operator/= Operator%= Operator&= Operator|= Operator^= Operator&lt;&lt;= Operator&gt;&gt;= Operator-&gt; Operator?? Operator
Tags : Add a tag

Community Content Add new content &amp;! Operator

Annotations hype8912 ... Stanley Roark | Edit

false

Edit: (Dave S.) There is no "&;;!" operator. You're simply using the binary "&;;" operator and the unary "!" operator and writing them without a space in between. The "&;;&;;" operator is the short-circuiting "&;;" operator, meaning that the right-side will not be evaluated if the left side is false, thus "&;;&;;" may provide performance benefits in cases where the right-side is expensive to evaluate. But more importantly, it's the semantic choice that you need to think about; e.g., in your example below that uses "&;;", even if the specified text doesn't start with the specified character !string.IsNullOrEmpty(character) will still be evaulated. This won't happen in the example that uses the "&;;&;;" operator, because the right side will not be evaluated if the left side is false. An unrelated note: StartsWith returns true for empty strings, and throws an exception for null references. (See http://msdn.microsoft.com/enus/library/baketfxw.aspx) Therefore, you should probably reverse the order of the operands, and optionally you only have to check character for null. I found that in Visual Studio 2010 with C# 3.5 that you can use &;;! operator. I ran my unit tests against it with both ways and they pass so I'm saying it works. There isn't any documentation on MSDN that this method of saying AND NOT really works. Haven't tested if there are any performance differences in using it yet but I'm sure it would be in the nanoseconds per thousands of iterations of difference. Below are some before and after examples of an extension method I used for trying it on. Before

Show History

publicstaticstringRemoveFirstChar(thisstring text, string character) { return text.StartsWith(character) &;;&;; !string.IsNullOrEmpty(character) ? text.Remove(0, 1) : text; }
After

publicstaticstringRemoveFirstChar(thisstring text, string character) { return text.StartsWith(character) &;;! string.IsNullOrEmpty(character) ? text.Remove(0, 1) : text; }
<i>Edit: (Dave S.)
Tags : Add a tag Flag as ContentBug

Edit

True

Send

Community Content
Click to Rate and Give Feedback

Give feedback on this content


1

Operator Keywords (C# Reference)


Visual Studio 2005

Used to perform miscellaneous actions such as creating objects, checking the run-time type of an object, obtaining the size of a type, and so forth. This section introduces the following keywords:

as Converts an object to a compatible type. is Checks the run-time type of an object. new new Operator Creates objects. new Modifier Hides an inherited member. new Constraint Qualifies a type parameter. sizeof Obtains the size of a type. typeof Obtains the System.Type object for a type. true true Operator Returns the boolean value true to indicate true and returns false otherwise. true Literal Represents the boolean value true. false false Operator Returns the Boolean value true to indicate false and returns false otherwise. false Literal Represents the boolean value false. stackalloc Allocates a block of memory on the stack.

The following keywords, which can be used as operators and as statements, are covered in the Statements section:

checked Specifies checked context. unchecked Specifies unchecked context.

Potrebbero piacerti anche