C#笔记(2)

命名规则:

1. 方法命名是用帕斯卡
2. 变量使用驼峰
3. is开头的都是bool I开头的都是接口 abs开头的是抽象类 
4. 弄一个变量在这里必须赋初值

char.IsDigit: 判断该char是否是10进制数值
字符串 –> 特殊的字符数组, 有下标 能遍历但是不能改

字符串:

1. 比较字符串(忽略大小写) 
2. 比较字符串(不忽略大小写)
3. 分割字符串
4. 查找字符串
5. 替换字符串 都换
6. 截取字符串
7. 空格处理

str.Trim() –> 去掉字符串前后空格

Substring(index, count) –> 截取之后的字符串
Substring(index) –> 截取之后的字符串
string str = “你好世界,呵呵哒”;
//string newStr = str.Substring(0, 3);
string newStr = str.Substring(5);
Console.WriteLine(newStr);

Replace(old,new) 都换
string str = “呵呵哒,呵呵哒你好世界,呵呵哒”;
string newStr = str.Replace(“呵呵哒”, “C# 编程”);
Console.WriteLine(newStr);

indexof/lastindexof 查找返回其所在下标位置 若-1 就代表该字符串没有对应的东东
string str = “C#编程我喜欢,C#编程我呵呵哒C#”;
// int tmpIndex = str.IndexOf(“C#”);
int tmpIndex = str.LastIndexOf(“C# “);
Console.WriteLine(tmpIndex);

split(char/char[]) 按照char/char[] 进行分割, 返回一个字符串数组
Compare: 0代表相等 非0 代表不相等 (strA,StrB, true) 忽略大小写
Equals true 相等
string.IsNullOrEmpty(str) 判断str是否是空对象或者是empty

out: 传出参数,在方法内部必须赋值
ref: 传入参数,在方法外部必须赋值

OOP编程
对象是一个抽象概念, 用代码表示就是new出来的基本都是对象
在unity中最能体现OOP三大特性中的哪一个?

命名空间就是包含功能类似的class的一个集合
public: 对象和子类都能访问
protected:子类可以访问,子类对象不能访问
private: 对象和子类都不能访问
base: 就是访问父类成员(公开和保护的)

二进制转十进制

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

namespace 二转十
{
class Program
{
///
/// 求次方
///
/// 那个数的次方
/// 几次方
/// number^cifang
static int CiFang(int number,int cifang)
{
int returnValue = 0; // 返回值
if (number == 0) // 0的次方都是0
{
return returnValue;
}
if (cifang == 0) // 任何数的0次方都是1
{
returnValue = 1;
return returnValue;
}
if (cifang == 1) // 任何数的1次方都是任何数
{
returnValue = number;
return returnValue;
}

        returnValue = number;
        // 
        for (int i = 1; i < cifang; i++)
        {
            //次方
            returnValue *= number;
        }
        //返回最终结果
        return returnValue;
        Console.ReadKey();
    }

    static void Main(string[] args)
    {


        int[] randomInts = new int[5];
        Random r = new Random();
        for (int i = 0; i < randomInts.Length; i++)
        {
            randomInts[i] = r.Next(0, 2);
        }
        //遍历
        foreach (int i in randomInts)
        {
            Console.Write(i);
        }
        Console.WriteLine();
        Console.WriteLine("====================");
        //弄一个10个数值 0~1
        int number = 0;
        //使用按位展开法逆向得出10进制
        for (int i = 0; i < randomInts.Length; i++)
        {
            //1. 获取一个二进制位
                   number += CiFang((randomInts[i] == 0) ? 0 : 2, randomInts.Length - 1 - i);

            // 2. 得知是N次方

        }
      Console.WriteLine(number);

    }
}

}

猜你喜欢

转载自blog.csdn.net/qq_43140883/article/details/82751532