C# 程序开始主要是写类和方法 的基本步骤和调用方法

时间:2023-03-10 00:14:34
C# 程序开始主要是写类和方法 的基本步骤和调用方法

主程序的使用方式以及调用方法
字段、属性、方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices; namespace demo学习控制
{
class Program
{ static void Main(string[] args)
{
Stuiden adsme = new Stuiden();
adsme.Bireday = Convert.ToDateTime("1983-02-03");
Console.WriteLine("我的年龄是:" + adsme.age + " " + "测试的结果完成了。");
Console.ReadKey(); //Console.Write("{0}*{1}", (1 == 2 ? 3 : 4) / 2, "10");
Console.ReadKey(); } }
}

类的信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace demo学习控制
{
class Stuiden
{
private DateTime bireday; public DateTime Bireday
{
get { return bireday; }
set { bireday = value; }
}
public int age
{
get { return DateTime.Now.Year - bireday.Year; }
} }
}

参照样例二

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace demo学习类的方法
{
class Program
{
static void Main(string[] args)
{ Class1 ads = new Class1();
Console.WriteLine("请输入您的名字");
ads.Usname1 = Console.ReadLine();
try
{
Console.WriteLine("请输入您的年龄");
ads.Age1 = Convert.ToInt32(Console.ReadLine()); }
catch
{
Console.WriteLine("您输入的信息有误,请重新输入");
return;
}
string info = ads.Getinfots();
//Console.WriteLine(info);
//Console.WriteLine(Uname);
Console.ReadKey(); }
}
}

样例二的类(读写访问权限自己修改)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace demo学习类的方法
{ class Class1
{ private string Usname; public string Usname1
{
get { return Usname; }
set { Usname = value; }
}
private int Age; public int Age1
{
get { return DateTime.Now.Year - Age; } }
public string Getinfots()
{ string info = string.Format("我的名字:{0},我的年龄:{1}", Usname, Age);
return info;
}
}
}