C#学习之路

基础

  • 创建控制台项目

在打印时如果遇到了终端打印不出时,在打印后加Console.ReadKey();

  • 注释
//行注释

/*
 *块注释
 *变量..
 *方法..
*/

///<summary>
///文档注释,会自动生成文档
///
///</summary>
  • 标识符(变量)

大小写敏感

包含:字母 _ 数字 @

不能以数字开头,不能有其他符号

避开关键字、避开类库

  • 数据类型

值类型:int  long byte decimal char double float

Console.WriteLine("int: {0}",sizeof(int));//sizeof(int)可以看int型占几个字节

引用类型:object 终极基类

int i=123;
object o=i;//装箱,将值类型数据(栈中),将i对应数据存入堆中,变量名o存入栈(指向堆中数据)
int j=(int)o;//拆箱,通过栈中存的变量名(存指向堆中的地址),然后从堆中数据取出赋值给j

动态类型:dynamic v=20; v="aaa" ;(引用类型中的一种)

指针类型:

  • 字符串
string str="hello world";
string str2="hello \n world";//\n 起到换行作用
string str3=@"hello \n world";//原生字符串
string str4="c:\\daming\\study\\sharp";//加上\转义字符
string str5=@"c:\daming\study\sharp";//等同于str4
Console.WriteLine(str);
  •  类型转换

隐式类型转换:

int a=100;
byte bx=12;
long a;
b=a;//悄悄地将int型转换成long型
b=bx;//悄悄地将byte型转换成long型
Console.WriteLine(b);

显式类型转换:

float f=3.14;
b=f;//会报错
b=(long)f;//正确,强制转换,即显式类型转换

调用Convert 类中的静态方法进行相应的类型转换

int a1=75;
Console.WriteLine(a1.ToString());//转换成字符串
  • 有关项目源码 

using System.Drawing;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            //Console.ReadKey();如果打印不出,加上这句代码
            Rect rect1 = new Rect(3, 4);//通过类实例化一个对象 实参
            Rect rect2 = new Rect(5, 6);
            rect1.Display();
            rect2.Display();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
   
    internal class Rect
    {
        //对象的属性
        private double length;
        private double width;
        public Rect(double l,double w)
        {
            //给对象属性赋值
            this.length = l;//对当前对象的引用
            this.width = w;//对当前对象的引用
        }
        //方法
        public double GetArea()
        {
            return length * width;
        }
        //信息输出
        public void Display()
        {
            Console.WriteLine("length={0};width={1};arda={2}", length,width,GetArea());//打印
        }
    }
}

操作符&流程控制

  • 终端键盘输入
string str=Console.ReadLine();
Console.WriteLine("键盘输入的内容是:",str);
  • 常量:const int iConst=111;//  iConst为常量,此时 iConst的值不可再更改

namespace OperatorsApplication
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Operator();
            Console.WriteLine("32:{0}",isPower(32));
        }
        static void Operator()
        {
            //+ - * /数组运算符
            //% 取余 模
            int a = 90;
            int b = 20;
            Console.WriteLine("a%b={0}", a % b);
            //++ -- 自增自减运算符 a++先运算后自增 ++a先自增后运算
            //关系运算符== < <= > >= !=
            if (a == b)
            {
                Console.WriteLine("a=b");
            }
            else
            {
                Console.WriteLine("a!=b");
            }
            //逻辑运算符 与&& 或|| 非!
            bool x = true;
            bool y = true;
            if (x && y)
            {
                Console.WriteLine("均为真");
            }
            //位运算符
            //按位与& 按位或| 取反~ 异或^(相同为0不同为1) 右移>> 左移<<
            int aa = 60;//0011 1100
            int bb = 13;//0000 1101
            Console.WriteLine("aa={0}", Convert.ToString(aa, 2));//打印二进制
            Console.WriteLine("aa&bb={0}", Convert.ToString(aa & bb, 2));
            Console.WriteLine("aa|bb={0}", Convert.ToString(aa | bb, 2));
            Console.WriteLine("~bb={0}", Convert.ToString(~ bb, 2));
            Console.WriteLine("aa^bb={0}", Convert.ToString(aa^bb, 2));
            Console.WriteLine("bb>>1={0}", Convert.ToString(bb>>1, 2));
            Console.WriteLine("bb<<1={0}", Convert.ToString(bb << 1, 2));

        }
        //判断一个数是不是2的n次方
        /*
         2^n 10进制
        123=1*10^2+2*10^1+3*10^0
        1(n个0)=1*2^n
        1000  2的n次方和此数-1得到的数相与 等于0,可以确定他是2的n次方
        &
        0111
        0000=0
         */
        public static bool isPower(int x)
        {
            if ((x&(x-1))==0)
            {
                return true;
            }
            else{
                return false;
            }
        }
        //找出数组只出现一次的数
        //1001^0000=1001 x^x=0 x^0=x
        public static int GetOne()
        {
            int[] nums = new int[] { 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 5 };
            int temp = 0;
            for(int i = 0; i < nums.Length; i++)
            {
                temp = temp ^ nums[i];
            }
            return temp;
        }
        //其他运算符
        public static void OtherOperators()
        {
            int a,b;
            a = 10;
            b = 20;
            int c = a == b ? a : b;//三目运算符:条件?满足:不满足

            string str = "hello";
            if(str is object)
            {
                Console.WriteLine("str是string的实例");//会输出
            }

        }
        //流程控制
        public static void Ctrl()
        {
            int a = 100;
            if (a == 10)
            {

            }
            else if(a == 20)
            {

            }
            else if (a == 30)
            {

            }
            else
            {

            }
            switch (a)
            {
                case 10:
                    Console.WriteLine();
                    break;
                case 20:
                    Console.WriteLine();
                    break;
                default:
                    switch (a)
                    {
                        
                    }
                    break;
            }
        }
    }
}
  • 循环
int a=10;
//while
while(a<20)
{
    a++;
}
//do while
do 
{
    a++;
}while(a<20);
//for循环
for(int i=0;i<10;i++)
{
    //输出
}
  • foreach循环
int[] fibArray=new int[]{1,2,4,6,7};
int index=0;
foreach(int item in fibArray)//用于数组循环,元素别名 in 循环数组
{
    Console.WriteLiine("当次循环:{0} 索引:{1}",item,index);
    index++;
}
  • 乘法表
///<summary>
///1*1=1
///1*2=2 2*2=4
///1*3=3 2*3=6 3*3=9
///...
///</summary>
public static void print()
{
    for(int i=1;i<10;i++)
    {
        for(int j=1;j<=i;j++)
        {
            Console.Write("{0}*{1}={2}",j,i,j*i);
        }
    }
    Console.WriteLine();
}
///<summary>
///******
///******
///******
///******
///******
///******
///</summary>
public static void star1()
{
    for(int i=1;i<6;i++)
    {
        for(int j=1;j<=6;j++)
        {
            Console.Write("*");
        }
    }
    Console.WriteLine();
}
///<summary>
///*
///**
///***
///****
///*****
///</summary>
public static void star2()
{
    for(int i=1;i<6;i++)
    {
        for(int j=1;j<=i;j++)
        {
            Console.Write("*");
        }
    }
    Console.WriteLine();
}
  • break:跳出当前循环
  • continue:到此位置不执行剩下语句直接进入下一次循环 跳过当次循环

访问控制&方法

  • 访问控制
private int i=1;//私有 只能在类内访问
public //公共 所有对象都可以访问
protected //该类和其子类能够访问
internal//程序集内部可以访问,程序外不能访问
  • 跨项目使用其他类
LoopApplication.TestAccess ta=new LoopApplication.TestAccess();

  •  static静态方法
using System;
namespace OperatorsApplication
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Operator();
            //Console.WriteLine("32:{0}",isPower(32));
            Number number = new Number();
            //调用类的非静态成员方法,必须使用类的实例来调用
            int r = number.FindMax(3, 5);
            Console.WriteLine("max={0}", r);
            //调用类的静态方法,必须使用类名来调用
            Number.Print();

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OperatorsApplication
{
    public class Number
    {
        public Number()
        {

        }
        public int FindMax(int a, int b)
        {
            int result;
            if (a > b)
            {
                result = a;
            }
            else
            {
                result = b;
            }
            return result;
        }
        public static void Print()
        {
            Console.WriteLine("hello world");
        }
    }
}
  • 递归方法
  public int fab(int n)
  {
      return n==1?1:n * fab(n - 1);
  }
  • 传参,引用传递(值传递不用ref)

  • 设置传参值 

数组&字符串

using System;
namespace ArraApplication
{
    internal class Program
    {
        public static void declareArray()
        {
            int[] nums = new int[] {1,2,3,4,5};//一维数组
            int[] nums1 = new int[5];
            nums1[0] = 1;
            nums1[1] = 2;
            nums1[2] = 3;
            nums1[3] = 4;
            nums1[4] = 5;

            foreach (int item in nums1)
            {
                Console.WriteLine(item + " ");
            }
        }
        public static void twoDim()
        {
            //二维数组
            int[,] nums = new int[2,3];//声明一个两行三列的数组
            nums[0, 0] = 1;
            //三维数组
            int[,,] nums1 = new int[2, 2, 3];
            //定义并初始化一个二维数组
            int[,] array = new int[3, 4]
            {
                {1,2,3,4 },{5,6,7,8},{7,8,3,1}
            };
            //去二维数组中的某个元素
            Console.WriteLine(array[1, 1]);
        }
        //交错数组,数组里的元素是数组
        public static void crossArray()
        {
            int[][] nums = new int[5][];
            Console.WriteLine(nums.Length);//5,因为是5个数组
            for (int i = 0; i < nums.Length; i++)
            {
                nums[i] = new int[4];//5行4列
            }
            int[][] scores = new int[5][]
            {
                new int[]{1,2,3,4 },
                new int[]{1,2,3,4 },
                new int[]{1,2,3,4 },
                new int[] { 1, 2, 3, 4 },
                new int[] { 1, 2, 3, 4 },

             };
            for(int i = 0; i < scores.Length; i++)//遍历数组
            {
                for(int j = 0; j < scores[i].Length; j++)
                {
                    Console.WriteLine("scores[{0}{1}]={2}", i, j, scores[i][j]);
                }
            }
        }
        public static double getAverage1(params int[] arr)//加了params后可以直接传数组数
        {
            double sum = 0;
            for(int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            return sum / arr.Length;
        }
        public static void teatGetAverage1()
        {
           // int[] arr = new int[] { 1, 2, 3 };
           // double avg = getAverage1(arr);
            double avg = getAverage1(1,2,3);
            Console.WriteLine(avg);
        }
        public static void ArrayClassTest()
        {
            int[] nums = new int[] { 1, 2, 33, 4 };
            for(int i = 0; i < nums.Length; i++)
            {
                Console.Write(nums[i] + "");
            }
            //反转数组
            Array.Reverse(nums);
            //排序
            Array.Sort(nums);

        }
        

    }
        static void Main(string[] args)
        {
           // declareArray();
            twoDim();
        }

    }
}
using System.Text;
using System;
namespace StringApplication
{
    internal class Program
    {
        public static void StringTest()
        {
            string str = "hello";
            Console.WriteLine(str);
            for(int i = 0; i < str.Length; i++)
            {
                Console.Write(str[i] + " ");
            }
            foreach(char item in str)
            {
                Console.Write(item + " ");
            }
        }
        //字符串拼接
        public static void add()
        {
            //第一种方式+
            string str1 = "hello";
            string str2 = "world";
            string str3 = str1 + str2;
            Console.WriteLine(str3);
            //定义一个字符串
            char[] letters = { 'h', 'e', 'l', 'l', 'o' };
            string greetings = new string(letters);
            Console.WriteLine(greetings);
            //拼接2
            string[] sArr = { "hello", "haerbin", "beijing" };
            Console.WriteLine(sArr[0] + sArr[1] + sArr[2]);

            Console.WriteLine(string.Join(" ", sArr));//用空格隔开
            //拼接3
            StringBuilder ss = new StringBuilder();
            ss.Append(sArr[0]).Append(sArr[1]).Append(sArr[2]);
            Console.WriteLine(ss);

            StringBuilder ss1 = new StringBuilder();
            foreach (string item in sArr)
            {
                ss1.Append(item);
            }
            Console.WriteLine(ss1);
        }
        //比较字符串
        public static void CompareString()
        {
            string str1 = "This is test";
            string str2 = "This is teat";
            int res = string.Compare(str1, str2);
            if (res < 0)
            {
                Console.WriteLine("str1<str2");
            }else if(res == 0)
            {

            }
            else if (res > 0)
            {

            }
            //string 类中 针对==进行了运算符重载,会进入if中(正常是不相等的)
            if (str1 == str2)
            {

            }
        }
        //包含,是否是子串
        public static void stringContains()
        {
            string str = "This is test";
            if (str.Contains("test"))
            {
                Console.WriteLine("包含");
            }
        }
        //截取字符串
        public static void getSubString()
        {
            string str = "Last night I dreamt";
            Console.WriteLine(str);
            string substr = str.Substring(5);//索引
            string substr1 = str.Substring(5,2);//索引 长度
            Console.WriteLine(substr);
        }
        static void Main(string[] args)
        {
            
        }
    }
}

 

 

 

 

 

 

  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值