using System; class ReferenceAndOutputParamtersTest { static void Main( string[] args ) { int y = 5; int z; Console.WriteLine( "Original value of y: {0}", y ); Console.WriteLine( "Original value of z: uninitialized " ); SquareRef( ref y ); // must use keyword ref SquareOut( out z ); // must use keyword out Console.WriteLine( "Value of y after SquareRef: {0}", y ); Console.WriteLine( "Value of z after SquareOut: {0} ", z ); Square( y ); Square( z ); Console.WriteLine( "Value of y after Square: {0}", y ); Console.WriteLine( "Value of z after Square: {0}", z ); } static void SquareRef( ref int x ) { x = x * x; } static void SquareOut( out int x ) { x = 6; x = x * x; } static void Square( int x ) { x = x * x; } }
Language Basics
Add Assigment for both number and string value
using System; class AddAssigment { static void Main() { //addition int a = 5; a += 6; Console.WriteLine(a); //string concatenation string s = "Hello"; s += " world."; Console.WriteLine(s); } }
Conditional operator and Math calculation
using System; class ConditionalOp { static double sinc(double x) { return x != 0.0 ? Math.Sin(x) / x : 1.0; } static void Main() { Console.WriteLine(sinc(0.2)); Console.WriteLine(sinc(0.1)); Console.WriteLine(sinc(0.0)); } }
logical exclusive-or
using System; class XOR { static void Main() { Console.WriteLine(true ^ false); // logical exclusive-or Console.WriteLine(false ^ false); // logical exclusive-or // Bitwise exclusive-or: Console.WriteLine("0x{0:x}", 0xf8 ^ 0x3f); } }
Iff operator in C#
//Octavalent Extension Methods //http://sdfasdf.codeplex.com/ //Library of extension methods for .Net create by Octavalent (www.octavalent.nl) namespace System.OctavalentExtensions { public static class BooleanExtensions { public static object Iff(this bool value, object valueIfTrue, object valueIfFalse) { if (value) return valueIfTrue; return valueIfFalse; } } }
Or Assignment
using System; class MainClass7 { static void Main() { int a = 0x0c; a |= 0x06; Console.WriteLine("0x{0:x8}", a); bool b = true; b |= false; Console.WriteLine(b); } }
And Assignment
using System; class AndAssignment { static void Main() { int a = 0x0c; a &= 0x06; Console.WriteLine("0x{0:x8}", a); bool b = true; b &= false; Console.WriteLine(b); } }