Variable default name

image_pdfimage_print

   


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.

image_pdfimage_print
   
 

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);
    }
}

    


Demonstrate using statement

image_pdfimage_print
   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Demonstrate using statement. 
 
using System; 
using System.IO; 
 
public class UsingDemo { 
  public static void Main() { 
    StreamReader sr = new StreamReader("test.txt"); 
 
    // Use object inside using statement. 
    using(sr) { 
      Console.WriteLine(sr.ReadLine()); 
      sr.Close(); 
    } 
 
    // Create StreamReader inside the using statement. 
    using(StreamReader sr2 = new StreamReader("test.txt")) { 
      Console.WriteLine(sr2.ReadLine()); 
      sr2.Close(); 
    } 
  } 
}

           
          


This version does not include the using System statement

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// This version does not include the using System statement. 
 
public class ExampleConsoleWriteLine { 
 
  // A C# program begins with a call to Main(). 
  public static void Main() { 
 
    // Here, Console.WriteLine is fully qualified. 
    System.Console.WriteLine("A simple C# program."); 
  } 
}