文章目录
-
-
-
- c#组成、类型、实例化、枚举
- 插箱与装箱、字节操作、字符串操作
- 流程控值语句
- 数据集合操作
- 属性、方法、参数类型、重载
- 结构和类
c#组成、类型、实例化、枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
class C {
public int value = 0;
}
public class stamp
{
public string name { get; set; }
public int Age { get; set; }
}
enum MyDate {
Sum=0,
Mon=1,
Tue=2,
Web=3,
Thu=4,
Fri=5,
Sat=6
}
static void Main(string[] args)
{
Console.WriteLine("Hello word");
int ls = 927;
byte shj = 255;
Console.WriteLine("ls={0}", ls);
Console.WriteLine("shj={0}", shj);
float theMySUM = 9.27f;
float theMuSums = 1.12F;
double myDou = 927d;
double mudou = 112D;
C r1 = new C();
C r2 = r1;
r2.value = 112;
Console.WriteLine("Values:{0},{1}", r1.value,r2.value);
stamp s1 = new stamp { name = "permist", Age = 25 };
int age = s1.Age;
string names = s1.name;
Console.WriteLine("name:{0},age:{1}", s1.name,s1.Age);
Console.WriteLine("name:{0},age:{1}", names,age);
int k = (int)DateTime.Now.DayOfWeek;
switch (k)
{
case (int)MyDate.Sum:Console.WriteLine("今天是星期日");break;
case (int)MyDate.Mon: Console.WriteLine("今天是星期一"); break;
case (int)MyDate.Tue: Console.WriteLine("今天是星期二"); break;
case (int)MyDate.Web: Console.WriteLine("今天是星期三"); break;
case (int)MyDate.Thu: Console.WriteLine("今天是星期四"); break;
case (int)MyDate.Fri: Console.WriteLine("今天是星期五"); break;
case (int)MyDate.Sat: Console.WriteLine("今天是星期六"); break;
}
Console.ReadLine();
}
}
}
插箱与装箱、字节操作、字符串操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
double x = 19810927.0112;
int y = (int)x;
Console.WriteLine(y);
int y2=Convert.ToInt32(x);
Console.WriteLine(y2);
int i = 2019;
object obj = i;
Console.WriteLine("1、i的值为{0} ;装箱的值对象为{1}", i, obj);
i = 987;
Console.WriteLine("2、i的值为{0} ;装箱的值对象为{1}", i, obj);
int j = 112;
object obj2 = j;
Console.WriteLine("1、j的值为{0} ;装箱的值对象为{1}", j, obj2);
int h = (int)obj2;
Console.WriteLine("2、j的值为{0} ;装箱的值对象为{1}", j, h);
int a1 = 0;
bool result = a1 is int;
Console.WriteLine(result);
string yesno= result ? "是int" : "不是int";
Console.WriteLine("判断结果是:{0}", yesno);
string[] phone = new string[5];
phone[0] = "1";
phone[1] = "2";
phone[2] = "3";
phone[3] = " ";
phone[4] = "5";
Console.WriteLine(phone[0]);
Console.WriteLine(phone);
Type mytype = typeof(int);
Console.WriteLine("类型:{0}",mytype);
string s1 = "你好" + "1";
string s2 = "你好";
if (String.Compare(s1, s2) == 0)
{
Console.WriteLine("相等");
}
else {
Console.WriteLine("不相等");
}
if (s1.Equals(s2))
{
Console.WriteLine("s1与s2相等");
}
else
{
Console.WriteLine("s1与s2不相等");
}
if (String.Equals(s1,s2))
{
Console.WriteLine("s1与s2相等");
}
else
{
Console.WriteLine("s1与s2不相等");
}
string news = String.Format("{0}{1}!!!!", s1, s2);
Console.WriteLine(news);
string s3 = s1.Substring(0, 2);
Console.WriteLine(s3);
string ss1 = "社会工业,团结发展";
String[] splistrings = new String[100];
splistrings=ss1.Split(',');
for (int q = 0; q < splistrings.Length; q++)
{
Console.WriteLine("item{0}:{1}",q,splistrings[q]);
}
string ss2=ss1.Insert(0, "我说得,");
string ss3=ss2.Insert(4, ",绝对");
Console.WriteLine(ss3);
string ss4 = "xxxxx";
string ss5 = ss4.PadLeft(6, '(');
Console.WriteLine(ss5);
String ss6 = ss5.PadRight(7, ')');
Console.WriteLine(ss6);
Console.WriteLine( ss6.Length+1);
Console.WriteLine(ss4.Remove(3));
Console.WriteLine(ss4.Remove(0,2));
string ss7 = String.Copy(ss4);
Console.WriteLine(ss7);
string b1 = ss4.Replace('x', 'c');
Console.WriteLine(b1);
StringBuilder hostStr = new StringBuilder("窗前明月光,疑是地上霜",100);
hostStr.Append("VS 举头望明月");
Console.WriteLine(hostStr);
hostStr.AppendFormat("{0:s}", " 低头思故乡");
Console.WriteLine(hostStr);
hostStr.Insert(0, "静夜思");
Console.WriteLine(hostStr);
hostStr.Replace("静夜思", "静夜思\t李白\t");
Console.WriteLine(hostStr);
hostStr.Remove(0, hostStr.Length - 6);
Console.WriteLine(hostStr);
Console.ReadLine();
}
}
}
流程控值语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string str1 = "A";
if (str1=="A")
{
Console.WriteLine("是的");
}
else if (str1=="B")
{
Console.WriteLine("不是");
}
switch (str1)
{
case "A":
Console.WriteLine("是的A");
break;
case "B":
Console.WriteLine("是的B");
break;
default:
Console.WriteLine("都不是的");
break;
}
while (str1.Length>0)
{
Console.WriteLine("不是空值");
break;
}
int i = 0;
do
{
Console.WriteLine("当前值是:{0}",i);
i++;
} while (i<10);
for (int j = 0; j < 10; j++)
{
Console.WriteLine("你好,{0}",j);
}
ArrayList alt = new ArrayList();
alt.Add("你");
alt.Add("的");
alt.Add("名");
alt.Add("字");
foreach (string item in alt)
{
Console.WriteLine(item);
}
for (int k = 0; k < 5; k++)
{
if (k==3)
{
continue;
}
Console.WriteLine(k);
}
ceshi1:
Console.WriteLine("goto语句测试");
goto ceshi1;
}
}
}
数据集合操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
int[] brr = new int[] { 1,2,3,4,5,6,7,8,9,10};
int[] crr = new int[] { 1, 22, 3, 43, 51, 6, 7, 8, 9, 10 };
int[,] erArr = new int[,] { { 1, 2 }, { 3, 4 } };
int[,] erBrr = { { 1, 2 }, { 3, 4 } };
int[,] erCrr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < brr.Length; i++)
{
Console.WriteLine(brr[i]);
}
for (int j = 0; j < erBrr.GetLength(0); j++)
{
for (int k = 0; k < erBrr.GetLength(1); k++)
{
Console.WriteLine(erBrr[j,k]);
}
}
foreach (int a3 in brr)
Console.WriteLine(a3);
Array.Sort(crr);
foreach (int a4 in crr)
Console.WriteLine(a4);
Array.Reverse(crr);
foreach (int a5 in crr)
Console.WriteLine(a5);
int[] drr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] err = new int[] { 11,12,13,14,15 };
int[] f = new int[drr.Length + err.Length];
Console.WriteLine(f.Length);
for (int q = 0; q < drr.Length; q++)
{
f[q] = drr[q];
}
for (int w = 0; w < err.Length; w++)
{
f[drr.Length+w] = err[w];
}
for (int ai = 0; ai < f.Length; ai++)
{
Console.WriteLine(f[ai]);
}
int [] a1 = new int[] { 1, 2, 3, 4, 5, 6, 7 };
ArrayList List = new ArrayList(a1);
Console.WriteLine(List.IsFixedSize);
Console.WriteLine(List.IsReadOnly);
Console.WriteLine(List.IsSynchronized);
Console.WriteLine(List[1]);
Console.WriteLine(List.SyncRoot);
List.Add(8);
Console.WriteLine(List.Count);
List.Insert(List.Count,9);
for (int array = 0; array < List.Count; array++)
{
Console.WriteLine(List[array]);
}
foreach (int num1 in List)
{
Console.WriteLine(num1);
}
List.Remove(8);
List.RemoveAt(0);
List.RemoveRange(0, 3);
foreach (int num1 in List)
{
Console.WriteLine(num1);
}
List.Clear();
Console.WriteLine("数组大小长度是{0}",List.Count);
Hashtable hash = new Hashtable();
hash.Add("id", "123");
hash.Add("name", "ss");
Console.WriteLine(hash.Count);
Console.WriteLine( hash["id"]);
Boolean iskeyvalue = hash.Contains("id");
Boolean isKey = hash.ContainsKey("id");
Console.WriteLine(isKey);
Boolean isValue = hash.ContainsValue("ss");
Console.WriteLine(isValue);
Console.WriteLine(hash.Keys);
foreach (String key in hash.Keys)
{
Console.WriteLine(hash[key].ToString());
}
foreach (String values in hash.Values)
{
Console.WriteLine(values.ToString());
}
hash.Remove("id");
hash.Clear();
}
}
}
属性、方法、参数类型、重载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
private string id = "";
private string name = "";
public string ID {
get {
return id;
}
set {
id = value;
}
}
public string Name
{
get { return name; }
set { name = value; }
}
public void Username(params string[] list) {
for (int i = 0; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
}
public void method(ref int i) {
i = 200;
}
public void methodOut(out int i)
{
i = 200;
}
public static void fangfa()
{
Console.WriteLine("静态方法,需要类名调用");
}
public void fangfa1() {
Console.WriteLine("非静态方法,通过实例调用");
}
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
double c=a+b;
return c;
}
static void Main(string[] args)
{
Program pro = new Program();
pro.ID = "123";
pro.Name = "zhang";
Console.WriteLine("ID为{0},name为{1}",pro.id,pro.name);
string[] s1 = new string[] { "zhang","王","李","你还哦" };
pro.Username(s1);
int values = 1;
pro.method(ref values);
Console.WriteLine(values);
int outValue;
pro.methodOut(out outValue);
Console.WriteLine(outValue);
Program.fangfa();
pro.fangfa1();
Console.WriteLine(pro.add(1,2));
Console.WriteLine(pro.add(1.1, 2.2));
}
}
}
结构和类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication3
{
public struct Rect {
public double w;
public double h;
public double Area() {
return w * h;
}
}
public class publics {
public Double a1 = 123.1;
public Double a2 = 123.123;
public Double a3;
public publics()
{
a3 = a1 + a2;
}
~publics()
{
Console.WriteLine("已经释放了publics的调用");
}
}
public class myClass{
private int a = 0;
private int b = 0;
public int A{
set {
a = value;
}
get {
return a;
}
}
public int B
{
set
{
b = value;
}
get {
return b;
}
}
public int Add() {
return a + b;
}
}
public class myClass1 : myClass {
private int c;
public int C
{
set
{
c = value;
}
get
{
return c;
}
}
public int Add2()
{
return A + B+C;
}
}
class Program
{
static void Main(string[] args)
{
Rect rect;
rect.w = 123.123;
rect.h = 1234.123;
Console.WriteLine("面积为{0}",rect.Area());
publics cs=new publics();
Console.WriteLine(cs.a3);
myClass my1 = new myClass();
my1.A = 123;
my1.B = 345;
Console.WriteLine(my1.Add());
myClass1 my2 = new myClass1();
my2.A = 123;
my2.B = 456;
my2.C = 120;
Console.WriteLine(my2.Add2());
}
}
}