using System; class LogOpsApp { public static void display(byte left, byte right, byte ans, string op) { string Lstr = null; string Rstr = null; string Astr = null; Lstr = Convert.ToString(left, 2); if (0 != right) Rstr = Convert.ToString(right, 2); else Rstr = "--------"; Astr = Convert.ToString(ans, 2); Console.WriteLine(" {0,8} {1} {2,8} {3,8} ",Lstr, op, Rstr, Astr); } static void Main(string[] args) { byte a, b, c, d, e, f, g; a = 255; b = 132; c = 85; byte OneOperand = 0; d = (byte)(a & b); display (a, b, d, "&"); e = (byte)(d | c); display (d, c, e, "|"); f = (byte)(e ^ a); display (e, a, f, "^"); g = (byte)~f; display (f, OneOperand, g, "~"); } }
Bit move operator
using System;
class Operators {
static void Main() {
int a = 256, b = 128, c, d;
c = a >> 1;
d = b << 1;
Console.WriteLine("{0}", c); //128
Console.WriteLine("{0}", d); //256
}
}
[/csharp]
Bit operator
using System; class Operators { static void Main() { int a = 10, b = 15, c = 20, d, e, h, i; bool f, g, j = (a == b), k; d = (a & b); e = (a | b); f = (j && (b == c)); g = (j || (b == c)); h = (a ^ b); i = ~b; k = !j; Console.WriteLine("{0}", d); //10 Console.WriteLine("{0}", e); //15 Console.WriteLine("{0}", f); //False Console.WriteLine("{0}", g); //False Console.WriteLine("{0}", h); //5 Console.WriteLine("{0}", i); //-16 Console.WriteLine("{0}", j); //False Console.WriteLine("{0}", k); //True } }
Bit Operators: move
using System;
class MainClass
{
static void Main(string[] args)
{
int i = 1;
for (int j = 0; j < 8; j++)
{
Console.WriteLine(
"{0} << {1} = {2}", i, j, i << j);
}
}
}
[/csharp]
null pointer
using System; public class MainClass { public static void Main(string[] args) { MyObject localObject = new MyObject(); Console.WriteLine("localObject.n is {0}", localObject.n); if (localObject.nextObject == null) { Console.WriteLine("localObject.nextObject is null"); } } public class MyObject { internal int n; internal MyObject nextObject; } }
C# Namespaces and Using
using ThatConsoleClass = System.Console; public class NamespacesUsing { public static void Main() { ThatConsoleClass.WriteLine("Hello"); } }
Demonstrate a namespace 2
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate a namespace. using System; // Bring Counter into view. using Counter; // Declare a namespace for counters. namespace Counter { // A simple countdown counter. class CountDown { int val; public CountDown(int n) { val = n; } public void reset(int n) { val = n; } public int count() { if(val > 0) return val--; else return 0; } } } public class NSDemo3 { public static void Main() { // now, CountDown can be used directly. CountDown cd1 = new CountDown(10); int i; do { i = cd1.count(); Console.Write(i + " "); } while(i > 0); Console.WriteLine(); CountDown cd2 = new CountDown(20); do { i = cd2.count(); Console.Write(i + " "); } while(i > 0); Console.WriteLine(); cd2.reset(4); do { i = cd2.count(); Console.Write(i + " "); } while(i > 0); Console.WriteLine(); } }