我的第一个C#程序
下面就是一个最简单的C#程序,注意namespace。
加入一个类成员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloCSharp
{
public class UnitConverter
{
int ratio;
public UnitConverter(int unitRatio) { ratio = unitRatio;}
public int Convert (int unit) { return unit * ratio;}
}
class Program
{
static void Main(string[] args)
{
int x = 42; // The ultimate answer of life.
UnitConverter feetToInchesConverter = new UnitConverter(12);
UnitConverter milesToFeetConverter = new UnitConverter(5280);
Console.WriteLine(feetToInchesConverter.Convert(30));
string message = "This is my first C# language. Learning C# makes me a better man.";
message += x.ToString();
Console.WriteLine("Hello Console." + x);
}
}
}
所以其实还是和Java很像的。
C#里面类成员的static 属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloCSharp
{
public class Panda
{
public string Name;
public static int Population;
public Panda ( string n = "HelloPanda")
{
Name = n;
Population = Population + 1; // This will automatically COUNT THE POPULATION OF the pandas. Pretty Cool Uh.
}
public int get_population()
{
return Population;
}
}
class Program
{
static void Main(string[] args)
{
int x = 42; // The ultimate answer of life.
UnitConverter feetToInchesConverter = new UnitConverter(12);
UnitConverter milesToFeetConverter = new UnitConverter(5280);
Panda myPanda = new Panda("Jimmy");
Panda yourPanda = new Panda();
Panda anotherPanda = new Panda("Dongavel");
Console.WriteLine(anotherPanda.get_population());
Console.WriteLine(yourPanda.get_population()); // guess what? this is outputing 3 instead of 2. All pandas share the same population. That's why it's called population.
Console.WriteLine(feetToInchesConverter.Convert(30));
Console.WriteLine("Hello Console." + x);
Console.WriteLine(x.GetType());
Console.ReadLine();
}
}
}
值 vs 引用
Struct是值类型,每一个实例或者名称在内存里面都有自己的内存空间。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloCSharp
{
public struct Point { public int x, y;}
class Program
{
static void Main(string[] args)
{
int x = 42; // The ultimate answer of life.
Console.WriteLine("Hello Console." + x);
Point x1 = new Point(); // without giving parameters. But the memory is allocated for this instance.
x1.x = 7;
x1.y = 6;
Point x2 = x1; // This will lead to a copy.
x1.x = 9;
Console.WriteLine(x1.x);
Console.WriteLine(x2.x);
Console.ReadLine();
}
}
}
如果将同样的东西定义成Class的话,就会变成引用。就算你定义了一个新的变量,然后用已有的Class来赋值,结果只会是很坑的添加了应用。最好的做法就是重载构建函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloCSharp
{
public struct Point { public int x, y;}
public class Point_reference {
public int x, y;
public Point_reference( Point_reference old_point)
{
x = old_point.x;
y = old_point.y;
}
public Point_reference()
{
// This null function is there to act as a position holder, because you overloaded the original Constructor function, you have to provide this null one if you want this class to be constructed without filling in anything.
}
}
class Program
{
static void Main(string[] args)
{
int x = 42; // The ultimate answer of life.
Console.WriteLine("Hello Console." + x);
Point x1 = new Point(); // without giving parameters. But the memory is allocated for this instance.
x1.x = 7;
Point x2 = x1; // This will lead to a copy.
x1.x = 9;
Point_reference new_x1 = new Point_reference();
new_x1.x = 9;
Point_reference new_x2 = new_x1; // This time it will be a reference. Just a name and be the attorney of new_x1.
Point_reference diff_x3 = new Point_reference(new_x1);
new_x2.x = 892;
diff_x3.x = 129;
Console.WriteLine(x1.x);
Console.WriteLine(x2.x);
Console.WriteLine(new_x1.x); // 892 because the new_x2 changed the value
Console.WriteLine(diff_x3.x);// independent instance, not to worry
Console.WriteLine(new_x2.x); // 892
Console.ReadLine();
}
}
}
多维矩阵
这个之后再说,因为我感觉我不会直接去用它……太懒了还是想用mathNet。
参数的修饰
C#里面有三个参数的修饰方法:
Modifier |
Passed by |
Direction |
(None) |
Value |
Going in |
ref |
Reference |
Going in |
out |
Reference |
Going out |
下面是几个例子。Python可以直接return好几个不同类型的结果,C#提供了一个out修饰词,用起来感觉差不多。注意传递参数的时候还是要加入相应的修饰词。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloCSharp
{
class Program
{
static void function_to_return_multiple_values(int x, ref int y, out int z, out int w)
{
z = x + y;
y = y + 1;
w = x - y;
}
static void Main(string[] args)
{
int x, y, z, w;
x = 9;
y = 6;
function_to_return_multiple_values(x, ref y, out z, out w);
Console.ReadLine();
}
}
}
Written with StackEdit.