吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1312|回复: 5
收起左侧

[学习记录] C# 学习笔记 参数使用

[复制链接]
Cool_Breeze 发表于 2021-3-2 11:15
Windows 7 没有装 visual studio 有些参数修饰符不兼容
Microsoft (R) Visual C# Compiler version 4.8.3761.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
装了frameWork4.8 提示我这是5.0版本!必须安装 visual studio 才能升级C# 版本吗?
[C#] 纯文本查看 复制代码
using System;

class Method
{
    static void Main()
    {
        // int x = 101;
        // ValueTransfer(x);
        // x = 101, GetHashCode = 101
        // Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
        
        // Student stu = new Student(){name = "Tim"};
        // x = Tim, GetHashCode = 46104728
        // Console.WriteLine("x = {0}, GetHashCode = {1}", stu.name, stu.GetHashCode());
        // ValueQuote(stu);
        // x = Tom, GetHashCode = 46104728
        // Console.WriteLine("x = {0}, GetHashCode = {1}", stu.name, stu.GetHashCode());
        
        // int x = 100;
        // x = 100, GetHashCode = 100
        // Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
        // ValueQuoteRef(ref x);
        // x = 1, GetHashCode = 1
        // Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
        
        // double x = default(double);
        // x = 0, GetHashCode = 0
        // Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
        // ValueQuoteOut(out x);
        // x = 100, GetHashCode = 100
        // Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
        
        
        // Borrow stu = new Borrow();
        // stu.ShowBook();
        // Book book = new Book();
        // book.title = "C#";
        // 查询书籍名
        // if (stu.Query(ref book))
        // {
            // book.borrow = "A, B, C, GIN";
            // Console.WriteLine("----------更改分割线----------");
        // }
        // else
        // {
            // Console.WriteLine("--------未更改分割线----------");
        // }
        // stu.ShowBook();
        
        // 不定长参数
        // Parameters(1,2,3,4,1,5,5);
        
        double pi = 3.14159666;
        // pi就是this修饰的那个参数
        double rpi = pi.Round(3);
        // 3.14159666 保留 3 位小数后为 3.142
        Console.WriteLine("{0} 保留 {1} 位小数后为 {2}", pi, 3, rpi);
    }
    
    class Student
    {
        public string name {get; set;}
    }
    
    class Book
    {
        public string title{get;set;}
        public string borrow{get;set;}
    }
    
    class Borrow
    {
        // books 存放着每本书的借用着
        Book[] books =
        {
            new Book(){title = "C#", borrow="A"},
            new Book(){title = "C", borrow="A"},
            new Book(){title = "Python", borrow="A, B"}
        };
        
        // ref 一个引用实例
        
        // Book temp = null;
        // public ref Book Query(string title)
        // {
            // for (int i = 0; i < books.Length; i++)
            // {
                // if (title = books[i].title)
                // {
                    // return ref title[i];
                // }
            // }
            // return ref temp;
        // }
        // Book temp = null;
        
        // 存储一个book地址的信息抛出去,使外部也能更改
        public bool Query(ref Book book)
        {
            for (int i = 0; i < books.Length; i++)
            {
                if (book.title == books[i].title)
                {
                    book = books[i];
                    return true;
                }
            }
            return false;
        }
        
        // 输出books信息
        public void ShowBook()
        {
            for (int i = 0; i < books.Length; i++)
            {
                Console.WriteLine("{0,-10} by {1}", books[i].title, books[i].borrow);
            }
        }
    }
    
    // 传值
    static void ValueTransfer(int x)
    {
        x = 100;
        // x = 100, GetHashCode = 100
        Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
    }
    
    // 引用一个自生就是引用类型的值
    static void ValueQuote(Student stu)
    {
        // 可以这样理解
        // 创建一个Student stu 然后将外面的stu 复制给内部的stu, stu(内) = stu(外)
        // 引用的目标地址相同
        stu.name = "Tom";
        // x = Tom, GetHashCode = 46104728
        Console.WriteLine("x = {0}, GetHashCode = {1}", stu.name, stu.GetHashCode());
    }
    
    // ref 参数修饰符 引用传值,[(in 参数修饰符)方法内只读 ]
    static void ValueQuoteRef(ref int x)
    {
        x = 1;
        // x = 1, GetHashCode = 1
        Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
    }
    
    // out 参数修饰符,方法内部必须对参数至少修改一次
    static void ValueQuoteOut(out double x)
    {
        try
        {
            x = Double.Parse(Console.ReadLine());
        }
        catch
        {
            x = 6;
        }
        
        // x = <input>, GetHashCode = <input>
        Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
    }
    
    static void Parameters(params int[] array)
    {
        // foreach (var a in array)
        // {
            // Console.WriteLine(a);
        // }
        // array[0] = 1
        // array[1] = 2
        // array[2] = 3
        // array[3] = 4
        // array[4] = 1
        // array[5] = 5
        // array[6] = 5
        for (int i = 0; i < array.Length; i++)
        {
            Console.WriteLine("array[{0}] = {1}", i, array[i]);
        }
    }
}

// 类扩展方法
// 扩展方法必须在顶级静态类中定义
public static class DoubleExtension
{
    // 必须是静态的。第一个参数必须使用 this 修饰符
    public static double Round(this double a, int digital)
    {
        return Math.Round(a, digital);
    }
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
yan182 + 1 + 1 学到了,学到了

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lili2312280 发表于 2021-3-2 11:24
学习大佬啊,不用安装也可以
 楼主| Cool_Breeze 发表于 2021-3-2 11:30
lili2312280 发表于 2021-3-2 11:24
学习大佬啊,不用安装也可以

看完一节课,总结一下。我去查查怎么升级!
Alex.Merceryj 发表于 2021-3-2 12:25
 楼主| Cool_Breeze 发表于 2021-3-2 12:44
Alex.Merceryj 发表于 2021-3-2 12:25
VS2019社区版本,用.net core吧

.net core兼容性更好吗?
Alex.Merceryj 发表于 2021-3-3 08:44
Cool_Breeze 发表于 2021-3-2 12:44
.net core兼容性更好吗?

跨平台的,大趋势。这个c#语言都是通用的,环境比较好
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 07:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表