
类的访问修饰符 修饰符 类名
{
类的成员
}
namespace code_1
{
class Test
{
private int id; //定义私有的整型字段 id
public readonly string name; //定义公有的只读字符串类型字段 name
internal static int age; //定义内部的静态的整型字段 age
private const string major = "计算机"; //定义私有的字符串类型常量 major
}
}
访问修饰符 修饰符 返回值类型 方法名(参数列表)
{
语句块;
}
namespace code_1
{
class Test
{
private int id; //定义私有的整型字段 id
public readonly string name; //定义公有的只读字符串类型字段 name
internal static int age; //定义内部的静态的整型字段 age
private const string major = "计算机"; //定义私有的字符串类型常量 major
private void PrintMsg()
{
Console.WriteLine("编号:" + id);
Console.WriteLine("姓名:" + name);
Console.WriteLine("年龄:" + age);
Console.WriteLine("专业:" + major);
}
//乘法
private double Multiply(double num1,double num2)
{
return num1 * num2;
}
}
}
public 数据类型 属性名
{
get
{
获取属性的语句块;
return 值;
}
set
{
设置属性得到语句块;
}
}
namespace code_1
{
class Book
{
private int id;
private string name;
private double price;
//设置图书编号属性
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
//设置图书名称属性
public string Name
{
get
{
return name;
}
}
//设置图书价格属性
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
}
}
public int Id{get; set;}
public string Name{get; set;}
public double Price{get; set;}
class User
{
public User(string name, string password, string tel)
{
this.Name = name;
this.Password = password;
this.Tel = tel;
}
public string Name { get; set; }
public string Password { get; set; }
public string Tel { get; set; }
public void PrintMsg()
{
Console.WriteLine("用户名:" + this.Name);
Console.WriteLine("密 码:" + this.Password);
Console.WriteLine("手机号:" + this.Tel);
}
}
class Program
{
static void Main(string[] args)
{
User user = new User("小明","123456","13131351111");
user.PrintMsg();
}
}
class SumUtils
{
public int Sum(int a,int b)
{
return a + b;
}
public double Sum(double a,double b)
{
return a + b;
}
public string Sum(string a,string b)
{
return a + b;
}
}
class LambdaClass
{
public static int Add(int a, int b) => a + b;
}
class FactorialClass
{
public static int Factorial(int n)
{
if(n == 0)
{
return 1;
}
return n * Factorial(n - 1);
}
}
class OuterClass
{
public class InnerClass
{
public string CardId { get; set; }
public string Password { get; set; }
public void PrintMsg()
{
Console.WriteLine("卡号为:" + CardId);
Console.WriteLine("密码为:" + Password);
}
}
}
OuterClass.InnerClass outInner = new OuterClass.InnerClass(); outInner.CardId = "622211100"; outInner.Password = "123456"; outInner.PrintMsg();
class Program
{
static void Main(string[] args)
{
Random rd = new Random();
Console.WriteLine("产生一个10以内的数:{0}", rd.Next(0, 10));
Console.WriteLine("产生一个0到1之间的浮点数:{0}", rd.NextDouble());
byte[] b = new byte[5];
rd.NextBytes(b);
Console.WriteLine("产生的byte类型的值为:");
foreach(byte i in b)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine("当前日期为:{0}", dt);
Console.WriteLine("当前时本月的第{0}天", dt.Day);
Console.WriteLine("当前是:{0}", dt.DayOfWeek);
Console.WriteLine("当前是本年度第{0}天", dt.DayOfYear);
Console.WriteLine("30 天后的日期是{0}", dt.AddDays(30));
}
}
class Program
{
static void Main(string[] args)
{
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(2019, 6, 1);
TimeSpan ts = dt2 - dt1;
Console.WriteLine("间隔的天数为{0}天", ts.Days);
}
}
上一篇:C#基本语法总结(2)
讨论数量:0