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); } }
Declaring a variable.
class MainClass { public static void Main() { short x; int y; double z = 0; x = 6; y = 10; z = x + y; System.Console.WriteLine("X = {0} Y = {1} Z = {2}", x, y, z); } }
Demonstrate using statement
/* 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
/* 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."); } }
Compare pointer
// compile with: /unsafe
class CompareOperators
{
unsafe static void Main()
{
int x = 234;
int y = 236;
int* p1 = &x;
int* p2 = &y;
System.Console.WriteLine(p1 < p2); System.Console.WriteLine(p2 < p1); } } [/csharp]
Pointer for struct
// compile with: /unsafe struct Point { public int x, y; } class MainClass12 { unsafe static void Main() { Point pt = new Point(); Point* pp = &pt; pp->x = 123; pp->y = 456; Console.WriteLine("{0} {1}", pt.x, pt.y); } }