c#筆記 函數片段 (從易到難) 全是案例

來源:互聯網
上載者:User
案例1:
static void Main(string[] args)
{
int i = 2;
int s = -3;
i = +s;//正數
Console.WriteLine(i.ToString());
i = -s;//變成負數
Console.WriteLine(i.ToString());、
}
案例2:
int i1 = 2;
int i2 = 3;
int i3 = 4;
i1 = -++i2 + i2 * ++i3 + i3;
Console.WriteLine(i1);//21
//運算子 ++(元素) +(一元) 最高,* / 第二 ,+ 第三, = 、*=第四,++(尾碼)最後
案例3:
static void Main(string[] args)
{
start:
int i = 5;
goto addval;
writeResul:
Console.WriteLine(i);
goto start;
addval:
i += 10;
goto writeResul; //goto語句形成了 無限迴圈
}


案例4:
public class MyLoveStory
{
public static void Main(string[] args)
{
Console.WriteLine("數字\t平方\t立方");
int[] ints = { 0,1,2,3,4,5,6,7,8,9};
for (int i = 0; i < ints.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}",ints[i],pingfang(ints[i]),lifang(ints[i]));
}

}
static int pingfang(int i)//起一個函數方法
{
return i * i;
}

static int lifang(int i)//起一個函數方法
{
return i * i * i;
}

}

案例5:
public class MyLoveStory
{
public static void Main(string[] args)
{
BookStore mybook = new BookStore();//申明一個變數,建立對象
mybook.Bookage = 23;
mybook.Bookname = "cisco";
string name = "Mr.w";
mybook.Welcome(name);//調用類的方法
}
}

class BookStore//建立一個類
{
private string bookname;
private int bookage;
public string Bookname
{
get { return bookname; }
set { this.bookname = value; }
}

public int Bookage
{
get { return bookage; }
set { this.bookage = value; }
}
public void Welcome(string name)//建立類的方法
{
Console.WriteLine("歡迎{0}進入圖書庫!",name);
}
}

案例6:
public class MyLoveStory
{
public static void Main(string[] args)
{
BookStore mybook = new BookStore("Nothing",0);//申明一個變數,建立對象,這裡要注意是用一個變數來引用一個對象
Console.WriteLine(mybook.Bookage+"\n"+mybook.Bookname);//輸出預設的類的變數的值

mybook.Bookage = 23;
mybook.Bookname = "cisco";
string name = "Mr.w";

Console.WriteLine(mybook.Bookage + "\n" + mybook.Bookname);//輸出賦值之後的變數的值
mybook.Welcome(name);//調用類的方法
}
}

class BookStore//建立一個類
{
private string bookname;
private int bookage;
public BookStore(string name,int age)//建構函式的作用就是初始化類裡的變數
{
bookname = name;
bookage = age;
}
public string Bookname
{
get { return bookname; }
set { this.bookname = value; }
}

public int Bookage
{
get { return bookage; }
set { this.bookage = value; }
}
public void Welcome(string name)//建立類的方法
{
Console.WriteLine("歡迎{0}進入圖書庫!",name);
}
}

案例7:
class Program
{
static void Main(string[] args)
{
Account account1 = new Account(50.00m);
Account account2 = new Account(-7.00m);
Console.WriteLine("{0:C}",account1.Balance);//50
Console.WriteLine("{0:C}", account2.Balance);
}
}

class Account
{
//變數使用前_,屬性使用大寫開頭
private decimal _balance; //私人變數
public Account(decimal i)//建構函式
{
Balance = i;//這裡是易錯的,如果你把Balance寫成小寫話,那麼下面的if就沒用了。。。
}

public void Credit(decimal amount)//無傳回值方法
{
_balance += amount;
}

public decimal Balance//屬性
{
get { return _balance; }
set
{
if (value >= 0)
{
this._balance = value;
}
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.