/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example2_5.cs is the same as Example2_4.csc, except myValue is assigned a value before it is referenced */ public class Example2_5 { public static void Main() { int myValue = 2; System.Console.WriteLine(myValue); // no error } }
Language Basics
Use new with a value type
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use new with a value type. using System; public class newValue { public static void Main() { int i = new int(); // initialize i to zero Console.WriteLine("The value of i is: " + i); } }
Create a 4-bit type called Nybble
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Create a 4-bit type called Nybble.
using System;
// A 4-bit type.
class Nybble {
int val; // underlying storage
public Nybble() { val = 0; }
public Nybble(int i) {
val = i;
val = val & 0xF; // retain lower 4 bits
}
// Overload binary + for Nybble + Nybble.
public static Nybble operator +(Nybble op1, Nybble op2)
{
Nybble result = new Nybble();
result.val = op1.val + op2.val;
result.val = result.val & 0xF; // retain lower 4 bits
return result;
}
// Overload binary + for Nybble + int.
public static Nybble operator +(Nybble op1, int op2)
{
Nybble result = new Nybble();
result.val = op1.val + op2;
result.val = result.val & 0xF; // retain lower 4 bits
return result;
}
// Overload binary + for int + Nybble.
public static Nybble operator +(int op1, Nybble op2)
{
Nybble result = new Nybble();
result.val = op1 + op2.val;
result.val = result.val & 0xF; // retain lower 4 bits
return result;
}
// Overload ++.
public static Nybble operator ++(Nybble op)
{
op.val++;
op.val = op.val & 0xF; // retain lower 4 bits
return op;
}
// Overload >.
public static bool operator >(Nybble op1, Nybble op2)
{
if(op1.val > op2.val) return true;
else return false;
}
// Overload <. public static bool operator <(Nybble op1, Nybble op2) { if(op1.val < op2.val) return true; else return false; } // Convert a Nybble into an int. public static implicit operator int (Nybble op) { return op.val; } // Convert an int into a Nybble. public static implicit operator Nybble (int op) { return new Nybble(op); } } public class NybbleDemo { public static void Main() { Nybble a = new Nybble(1); Nybble b = new Nybble(10); Nybble c = new Nybble(); int t; Console.WriteLine("a: " + (int) a); Console.WriteLine("b: " + (int) b); // use a Nybble in an if statement if(a < b) Console.WriteLine("a is less than b "); // Add two Nybbles together c = a + b; Console.WriteLine("c after c = a + b: " + (int) c); // Add an int to a Nybble a += 5; Console.WriteLine("a after a += 5: " + (int) a); Console.WriteLine(); // use a Nybble in an int expression t = a * 2 + 3; Console.WriteLine("Result of a * 2 + 3: " + t); Console.WriteLine(); // illustrate int assignment and overflow a = 19; Console.WriteLine("Result of a = 19: " + (int) a); Console.WriteLine(); // use a Nybble to control a loop Console.WriteLine("Control a for loop with a Nybble."); for(a = 0; a < 10; a++) Console.Write((int) a + " "); Console.WriteLine(); } } [/csharp]
Two reference type variables may refer (or point) to the same object
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ /* Refs1.cs - Shows that two reference type variables may refer (or point) to the same object. Changing the object using one variable changes the object for the other variable. Compile this program with the following command line: csc refs1.cs */ namespace nsReference { using System; public class Refs1 { static public void Main () { clsClass first = new clsClass (42); clsClass second = first; second.m_Var /= 2; Console.WriteLine ("first.m_Var = " + first.m_Var); } } class clsClass { public clsClass (int var) { m_Var = var; } public int m_Var; } }
Heap and Stack Memory
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_7___References__Pointers_and_Memory_Management { public class HeapandStackMemory { static void Main(string[] args) { MyClass ThisClass = new MyClass(); } } public class MyClass { public int MyInt; private long MyLong; public void DoSomething() { Console.WriteLine(MyInt); Console.WriteLine(MyLong); } } }
Variable default name
using System; class DefValObject { // Here are a number of fields... public sbyte theSignedByte; public byte theByte; public short theShort; public ushort theUShort; public int theInt; public uint theUInt; public long theLong; public ulong theULong; public char theChar; public float theFloat; public double theDouble; public bool theBool; public decimal theDecimal; public string theStr; public object theObj; public static int Main(string[] args) { DefValObject v = new DefValObject(); // Print out default values. Console.WriteLine("bool: {0}", v.theBool); Console.WriteLine("byte: {0}", v.theByte); Console.WriteLine("char: {0}", v.theChar); Console.WriteLine("decimal: {0}", v.theDecimal); Console.WriteLine("double: {0}", v.theDouble); Console.WriteLine("float: {0}", v.theFloat); Console.WriteLine("int: {0}", v.theInt); Console.WriteLine("long: {0}", v.theLong); Console.WriteLine("object: {0}", v.theObj); Console.WriteLine("short: {0}", v.theShort); Console.WriteLine("signed byte: {0}", v.theSignedByte); Console.WriteLine("string: {0}", v.theStr); Console.WriteLine("unsigned int: {0}", v.theUInt); Console.WriteLine("unsigned long: {0}", v.theULong); Console.WriteLine("unsigned short: {0}", v.theUShort); return 0; } }
Initializing a variable.
using System; class MainClass{ public static void Main() { // Initialize a variable at declaration short x = 5; // Initialize a variable as a copy of another int y = x; double z = y + 10.25; int a = (int)z; Console.WriteLine("X = {0} Y = {1} Z = {2}", x, y, z); Console.WriteLine("A = {0}", a); } }