# C# Programming/Keywords/operator

The `operator` keyword allows a class to overload arithmetic and cast operators:

```public class Complex
{
private double re, im;

public double Real
{
get { return re; }
set { re = value; }
}

public double Imaginary
{
get { return im; }
set { im = value; }
}

public static Complex operator +(Complex c1, Complex c2)
{
return new Complex() { Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary };
}

public static Complex operator -(Complex c)
{
return new Complex() { Real = -c.Real, Imaginary = -c.Imaginary };
}

public static implicit operator double(Complex c)
{
// return the modulus: sqrt(x^2 + y^2)
return Math.Sqrt(Math.Pow(c.Real, 2) + Math.Pow(c.Imaginary, 2));
}

public static explicit operator string(Complex c)
{
// we should be overloading the ToString() method, but this is just a demonstration
return c.Real.ToString() + " + " + c.Imaginary.ToString() + "i";
}
}

public class StaticDemo
{
public static void Main()
{
Complex number1 = new Complex() { Real = 1, Imaginary = 2 };
Complex number2 = new Complex() { Real = 4, Imaginary = 10 };
Complex number3 = number1 + number2; // number3 now has Real = 5, Imaginary = 12

number3 = -number3; // number3 now has Real = -5, Imaginary = -12
double testNumber = number3; // testNumber will be set to the absolute value of number3
Console.WriteLine((string)number3); // This will print "-5 + -12i".
// The cast to string was needed because that was an explicit cast operator.
}
}
```

C# Keywords
 `abstract` `as` `base` `bool` `break` `byte` `case` `catch` `char` `checked` `class` `const` `continue` `decimal` `default` `delegate` `do` `double` `else` `enum` `event` `explicit` `extern` `false` `finally` `fixed` `float` `for` `foreach` `goto` `if` `implicit` `in` `int` `interface` `internal` `is` `lock` `long` `namespace` `new` `null` `object` `operator` `out` `override` `params` `private` `protected` `public` `readonly` `ref` `return` `sbyte` `sealed` `short` `sizeof` `stackalloc` `static` `string` `struct` `switch` `this` `throw` `true` `try` `typeof` `uint` `ulong` `unchecked` `unsafe` `ushort` `using` `var` `virtual` `void` `volatile` `while`
Special C# Identifiers (Contextual Keywords)
 `add` `alias` `async` `await` `dynamic` `get` `global` `nameof` `partial` `remove` `set` `value` `when` `where` `yield`
Contextual Keywords (Used in Queries)
 `ascending` `by` `descending` `equals` `from` `group` `in` `into` `join` `let` `on` `orderby` `select` `where`
{"ft_lang_label":"__label__en","ft_lang_prob":0.6418564,"math_prob":0.8439299,"size":1486,"snap":"2019-51-2020-05","text_gpt3_token_len":401,"char_repetition_ratio":0.18960863,"word_repetition_ratio":0.07114624,"special_character_ratio":0.31965005,"punctuation_ratio":0.17786561,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9964305,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-15T21:05:26Z\",\"WARC-Record-ID\":\"<urn:uuid:f66d3afd-e400-411b-a5dc-42f848455a5a>\",\"Content-Length\":\"47459\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f1d6a56d-f2ad-4f29-8d03-1b508e921d3e>\",\"WARC-Concurrent-To\":\"<urn:uuid:0d19be10-1399-4dbe-b1e7-8bdd8680b7f2>\",\"WARC-IP-Address\":\"208.80.154.224\",\"WARC-Target-URI\":\"https://en.m.wikibooks.org/wiki/C_Sharp_Programming/Keywords/operator\",\"WARC-Payload-Digest\":\"sha1:35GNVX5KJKZHSRFZH5XAZMXXP2RN5VX2\",\"WARC-Block-Digest\":\"sha1:FVUI2RQACAMGVR5VXXV4JN272SY4LAU6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575541310866.82_warc_CC-MAIN-20191215201305-20191215225305-00472.warc.gz\"}"}