其實(shí)我本身對于c#這門語言不是很有興趣,但是因?yàn)閷W(xué)校的實(shí)訓(xùn)課程要求使用這門語言所以就只好跟著學(xué)習(xí)了。經(jīng)過一周的學(xué)習(xí),也學(xué)了個(gè)大概 以下就做一個(gè)學(xué)習(xí)筆記,方便日后要用的時(shí)候回來再看一下。
其實(shí)c#和c++有很多類似之處 其基本語法和c++幾乎完全一樣 下面先說一下和c++的不同之處
首先是 它的輸出部分
Console.WriteLine("姓名:{0} 年齡{1}",name,age);
用控制臺輸出{0} {1}分別代表逗號后面的第幾個(gè)位置
c#的 foreach循環(huán)
int count = 0;
foreach (int element in fibarray)
{
count += 1;
System.Console.WriteLine("Element #{0}: {1}", count, element);
}
對于數(shù)組的循環(huán)非常好用
c#重載
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace c2
{
class Complex
{
private int real;
private int imag;
public Complex(int a = 0,int b = 0)
{
real = a;
imag = b;
}
static public Complex operator+(Complex a,Complex b)
{
return new Complex(a.real+b.real,a.imag+b.imag);
}
static public Complex operator -(Complex a, Complex b)
{
return new Complex(a.real - b.real, a.imag - b.imag);
}
static public Complex operator -(Complex a)
{
return (new Complex(-a.real, -a.imag));
}
public void display()
{
Console.WriteLine("{0}i+{1}j", real, imag);
}
}
class Program
{
static void Main(string[] args)
{
Complex x = new Complex(2,-3);
Complex y = new Complex(-1, 5);
Complex z;
z = x + y;
z.display();
z = -z;
z.display();
z = y - x;
z.display();
}
}
}
其中的基本語法和c++差不多 但是需要用到重載函數(shù)要放到static中
并且沒有友元函數(shù)的重載了。
c++的雙目運(yùn)算符通常都用放到友元函數(shù)中間
引用存儲的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace c1
{
class Person
{
private string name;
private int age;
public Person(string a,int b)
{
name = a;
age = b;
}
public void Setname(string a)
{
name = a;
}
public void Setage(int a)
{
age = a;
}
public void display()
{
Console.WriteLine("姓名:{0} 年齡{1}",name,age);
}
}
class Program
{
static void Main(string[] args)
{
Person a = new Person("wang", 13);
Person b;
a.display();
b = a;
b.Setname("tang");
a.display();
}
}
}
引用存儲
二者共用一塊存儲空間
當(dāng)改變其中一個(gè)的值 另一個(gè)值也會(huì)改變