Computer Science, asked by Aafreen5802, 1 year ago

What are unary operators in C#?

Answers

Answered by saranyaammu3
1

Answer:

Unary operator is operators that act upon a single operand to produce a new value. The unary operators are as follows:

Operators

Indirection operator (*)

It operates on a pointer variable and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer.

Address-of operator (&)

The unary address-of operator (&) takes the address of its operand. The operand of the address-of operator can be either a function designator or an l-value that designates an object that is not a bit field and is not declared with the register storage-class specifier.

Unary plus operator (+)

The result of the unary plus operator (+) is the value of its operand. The operand to the unary plus operator must be of an arithmetic type.

Unary negation operator (-)

The - (unary minus) operator negates the value of the operand. The operand can have any arithmetic type. The result is not an lvalue.

Logical negation operator (!)

The logical negation operator (!) reverses the meaning of its operand. The operand must be of arithmetic or pointer type (or an expression that evaluates to arithmetic or pointer type). The operand is implicitly converted to type bool.

One's complement operator (~)

The one's complement operator sometimes called the "bitwise complement" or "bitwise NOT" operator, produces the bitwise one's complement of its operand. The operand must be of integral type.

Prefix increment operator (++)

The prefix increment operator ( ++ ) adds one to its operand; this incremented value is the result of the expression. The operand must be an l-value not of type const. The result is an l-value of the same type as the operand.

Prefix decrement operator (--)

The prefix decrement operator ( -- ) subtracts one from its operand; this decremented value is the result of the expression. The operand must be an l-value not of type const. The result is an l-value of the same type as the operand.

Cast operator ()

A type cast provides a method for explicit conversion of the type of an object in a specific situation. The compiler treats cast-expression as type type-name after a typecast has been made.

sizeof operator

It is a compile-time unary operator which can be used to compute the size of its operand.

new operator

It is a memory allocation operator that is used to allocate memory dynamically.

delete operator

It is a memory allocation operator that is used to deallocate memory that was dynamically allocated.

These operators have right-to-left associativity. Unary expressions generally involve syntax that precedes a postfix or primary expression.

Let's look at an example of the -(minus) and casting() unary operators.

#include<iostream>

using namespace std;

int main() {

  int x;

  float y = 1.23;

  x = (int) y;

  x = -x;

  cout << x;

  return 0;

}

This gives the output:

-1

Explanation:

Answered by StaceeLichtenstein
1

The unary operator works on the single operands in c# programming language

Explanation:

The unary operator works on the single variable or single operands there are two type of unary operator in c# programming language

  • Increment operator
  • Decrement operator
  • Increment operator are those which increment the value of variable by 1 .
  • Decrement operator are those which decrement the value of variable by 1 .

Following are the example of unary operator in C#

using System;  // namespace

using System.Collections.Generic;  // namespace

using System.Linq;  // namespace

using System.Text;  // namespace

namespace test

{

   class Prgm

   {

       static void Main(string[] args)  // main method

       {

           int k = 15; // variable Initialization

           Console.WriteLine(k) ;// print the value of k

             k--; // decremented the value of k by one

         Console.WriteLine(k) ;// print the value of k

    k++; // incremented the value of k by on

 Console.WriteLine(k) ;// print the value of k

           Console.ReadLine();

       }

   }

}

Output:

14

15

Learn More:

  • https://brainly.in/question/8811721
Similar questions