/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// RefType.cs -- Demonstrate declaring a reference type variable
// and creating an object the variable will reference.
//
// Compile this program with the following command line:
// C:>csc RefType.cs
using System;
using System.IO;
namespace nsRefType
{
public class RefType123
{
static public void Main ()
{
// Declare the reference type variable
FileStream strm;
// Create the object the variable will reference
strm = new FileStream ("./File.txt",
FileMode.OpenOrCreate,
FileAccess.Write);
}
}
}
Language Basics
Create an implication operator in C#
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Create an implication operator in C#.
using System;
public class Implication {
public static void Main() {
bool p=false, q=false;
int i, j;
for(i = 0; i < 2; i++) { for(j = 0; j < 2; j++) { if(i==0) p = true; if(i==1) p = false; if(j==0) q = true; if(j==1) q = false; Console.WriteLine("p is " + p + ", q is " + q); if(!p | q) Console.WriteLine(p + " implies " + q + " is " + true); Console.WriteLine(); } } } } [/csharp]
Using casts in an expression
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using casts in an expression.
using System;
public class CastExpr {
public static void Main() {
double n;
for(n = 1.0; n <= 10; n++) { Console.WriteLine("The square root of {0} is {1}", n, Math.Sqrt(n)); Console.WriteLine("Whole number part: {0}" , (int) Math.Sqrt(n)); Console.WriteLine("Fractional part: {0}", Math.Sqrt(n) - (int) Math.Sqrt(n) ); Console.WriteLine(); } } } [/csharp]
A promotion surprise
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A promotion surprise!
using System;
public class PromDemo {
public static void Main() {
byte b;
b = 10;
b = (byte) (b * b); // cast needed!!
Console.WriteLine("b: "+ b);
}
}
Demonstrate casting
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate casting.
using System;
public class CastDemo {
public static void Main() {
double x, y;
byte b;
int i;
char ch;
uint u;
short s;
long l;
x = 10.0;
y = 3.0;
// cast an int into a double
i = (int) (x / y); // cast double to int, fractional component lost
Console.WriteLine("Integer outcome of x / y: " + i);
Console.WriteLine();
// cast an int into a byte, no data lost
i = 255;
b = (byte) i;
Console.WriteLine("b after assigning 255: " + b +
" -- no data lost.");
// cast an int into a byte, data lost
i = 257;
b = (byte) i;
Console.WriteLine("b after assigning 257: " + b +
" -- data lost.");
Console.WriteLine();
// cast a uint into a short, no data lost
u = 32000;
s = (short) u;
Console.WriteLine("s after assigning 32000: " + s +
" -- no data lost.");
// cast a uint into a short, data lost
u = 64000;
s = (short) u;
Console.WriteLine("s after assigning 64000: " + s +
" -- data lost.");
Console.WriteLine();
// cast a long into a uint, no data lost
l = 64000;
u = (uint) l;
Console.WriteLine("u after assigning 64000: " + u +
" -- no data lost.");
// cast a long into a uint, data lost
l = -12;
u = (uint) l;
Console.WriteLine("u after assigning -12: " + u +
" -- data lost.");
Console.WriteLine();
// cast an int into a char
b = 88; // ASCII code for X
ch = (char) b;
Console.WriteLine("ch after assigning 88: " + ch);
}
}
This program attempts to declared a variable in an inner scope
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
This program attempts to declared a variable
in an inner scope with the same name as one
defined in an outer scope.
*** This program will not compile. ***
*/
using System;
public class NestVar {
public static void Main() {
int count;
for(count = 0; count < 10; count = count+1) { Console.WriteLine("This is count: " + count); int count; // illegal!!! for(count = 0; count < 2; count++) Console.WriteLine("This program is in error!"); } } } [/csharp]
Demonstrate lifetime of a variable
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate lifetime of a variable.
using System;
public class VarInitDemo {
public static void Main() {
int x;
for(x = 0; x < 3; x++) { int y = -1; // y is initialized each time block is entered Console.WriteLine("y is: " + y); // this always prints -1 y = 100; Console.WriteLine("y is now: " + y); } } } [/csharp]