2013年7月14日 星期日

C# 操作、型式和變數:Operators, Types, and Variables


 C#中,一定要宣告變數的型態!
Example 1: Boolean
using System;

class
Booleans
{
    public static void Main()
    {
        bool content = true;
        bool noContent = false;

        Console.WriteLine("It is {0} that C# Station provides C# programming language content.", content);
        Console.WriteLine("The statement above is not {0}.", noContent);
    }
}
 
 Example 2:
using System;

class Unary
{
    public static void Main()
    {
        int unary = 0;
        int preIncrement;
        int preDecrement;
        int postIncrement;
        int postDecrement;
        int positive;
        int negative;
        sbyte bitNot;
        bool logNot;

        preIncrement = ++unary; // unary+1
後印出
        Console.WriteLine("pre-Increment: {0}", preIncrement); //
印出1unary = 1

        preDecrement = --unary; // unary-1
後印出
        Console.WriteLine("pre-Decrement: {0}", preDecrement); // 印出0unary = 0
        postDecrement = unary--; // unary
後印出-1
          Console.WriteLine("Post-Decrement: {0}", postDecrement); //印出0 unary = -1
        postIncrement = unary++; // unary
後印出+1
          Console.WriteLine("Post-Increment: {0}", postIncrement); //印出-1 unary = 0
        Console.WriteLine("Final Value of Unary: {0}", unary);

        positive = -postIncrement;
        Console.WriteLine("Positive: {0}", positive);

        negative = +postIncrement;
        Console.WriteLine("Negative: {0}", negative);

        bitNot = 0;
        bitNot = (sbyte)(~bitNot);
        Console.WriteLine("Bitwise Not: {0}", bitNot);

        logNot = false;
        logNot = !logNot;
        Console.WriteLine("Logical Not: {0}", logNot);
    }
}
Example 3: Array
 using System;

class Array
{
    public static void Main()
    {
        int[] myInts = { 5, 10, 15 }; //
向量myInts,由int[]接起來為整數!
        bool[][] myBools = new bool[2][]; //
jagged array,向量裡的一個向量(an array of arrays)。很像MATLAB裡的cell
        // 產生三個向量在myInts裡
        myBools[0] = new bool[2]; // 初始化(new),產生boolean值,有2個元素
        myBools[1] = new bool[1]; 
// 初始化(new),產生boolean值,有1個元素
        double[,] myDoubles = new double[2, 2];
        string[] myStrings = new string[3];

        Console.WriteLine("myInts[0]: {0}, myInts[1]: {1}, myInts[2]: {2}", myInts[0], myInts[1], myInts[2]);

        myBools[0][0] = true; //
myBools[0]有2個元素,其元素再加用一個[]來指定!
          myBools[0][1] = false;
        myBools[1][0] = true;
        Console.WriteLine("myBools[0][0]: {0}, myBools[1][0]: {1}", myBools[0][0], myBools[1][0]);

        myDoubles[0, 0] = 3.147;
        myDoubles[0, 1] = 7.157;
        myDoubles[1, 1] = 2.117;
        myDoubles[1, 0] = 56.00138917;
        Console.WriteLine("myDoubles[0, 0]: {0}, myDoubles[1, 0]: {1}", myDoubles[0, 0], myDoubles[1, 0]);

        myStrings[0] = "Joe";
        myStrings[1] = "Matt";
        myStrings[2] = "Robert";
        Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]);

    }
}
And here's the output:
myInts[0]: 5, myInts[1]: 10, myInts[2]: 15
myBools[0][0]: true, myBools[1][0]: true
myDoubles[0, 0]: 3.147, myDoubles[1, 0]: 56.00138917
myStrings[0]: Joe, myStrings[1]: Matt, myStrings[2]: Robert


0 意見: