RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-514475

Humble Newbie's questions

Martin Hope
Humble Newbie
Asked: 2022-10-05 09:35:18 +0000 UTC

我请求帮助以使用方法和类的示例来理解单一责任原则

  • 1
  1. 有一种方法可以返回一个表格,其中包含在程序文件夹中找到的 csv 文件的名称:

     public static List<string> GetListOfTables()
     {
         var tables = new List<string>();
    
         do
         {
             tables = Directory.GetFiles(".", "*.csv").Select(file => Path.GetFileName(file)).ToList();
    
             if (tables.Count == 0)
             {
                 Output.Invoke("\nNo .csv files are found, " +
                     "please put a table csv file in the program folder and press <Enter> to continue");
    
                 while (Console.ReadKey(true).Key != ConsoleKey.Enter) { }
             }
         }
         while (tables.Count == 0);
    
         return tables;
     }
    

它具有内置验证(而不是排除),如果没有找到文件,用户需要将文件复制到文件夹并按下一个键。

这个方法有单一职责(最终返回工作表,检查只是一个检查)还是多个(返回工作表并检查用户操作的正确性)?

是否可以使用验证块创建单独的ValidationCsvFiles方法,然后将其包含或委托给 main 方法?

  1. 有一个类返回包含特定文件数据的工作表
static class CsvParser
    {
        public static List<string> GetListOfTables() { }

        public static List<List<object>> GetTable(string tableName) { }
    }

此类具有单一职责(导致返回带有数据的工作表,带有文件列表的工作表是辅助工具)或多个(返回带有数据的工作表和带有文件的工作表)

毕竟,一个有文件的工作表可以在另一个类中实现吗?

c#
  • 1 个回答
  • 60 Views
Martin Hope
Humble Newbie
Asked: 2022-09-29 12:45:18 +0000 UTC

在 setter 或类构造函数中设置属性值在哪里更正确?

  • 0
public abstract class Parser
{
    protected List<string> Tables { get; set; }
    protected string TableName { get; }
    public List<List<object>> Table { get; protected set; }

    public Parser()
    {
        Tables = GetListOfTables();
        Program.PrintCollection(Tables);
        TableName = Program.GetUserChoose(Tables);
        Table = GetTable();
    }

    protected abstract List<string> GetListOfTables();
    protected abstract List<List<object>> GetTable();
}

在构造函数中或直接在属性设置器中为属性赋值的正确方法是什么?在我看来,立即在属性中更合乎逻辑,但您也可以在构造函数中设置它。也许有一些潜规则?查看内置类的描述,并没有发现很多立即设置属性的情况。

c# properties
  • 2 个回答
  • 75 Views
Martin Hope
Humble Newbie
Asked: 2022-09-26 22:10:00 +0000 UTC

在已经检查 null 的情况下的 C# 可为 null 的警告

  • 0
if (currentAsm[i].IsClass && currentAsm[i].Namespace != null 
                && currentAsm[i].Namespace.Contains("parse", StringComparison.InvariantCultureIgnoreCase))  

不清楚开发环境为何突出

currentAsm[i].命名空间

作为可能为 null 的引用的取消引用,尽管在相同条件下检查 null

在使用 linq 的类似示例中,当检查 null 时没有可空警告

.Where(type => type.FullName != null && type.Namespace != null && type.Namespace.Contains("parse", StringComparison.InvariantCultureIgnoreCase))

而第二个问题,如何最好地安排在Where中的多个条件的转移?由于没有立即使用选项卡转换到新行,您是按选项卡本身还是按 VS 建议的那样操作?例如,

在此处输入图像描述

或者像这样(我自己用标签格式化): 在此处输入图像描述

c#
  • 1 个回答
  • 53 Views
Martin Hope
Humble Newbie
Asked: 2022-09-18 21:49:40 +0000 UTC

c# StreamReader 和 FileStream 的区别

  • 0

我不明白为什么

private static GoogleCredential? _credential;
private static readonly string[] _scopes = {SheetsService.Scope.Spreadsheets};

private static GoogleCredential GetCredential()
        {
            var stream = new StreamReader(File.OpenRead("client_secrets.json"));

            _credential = GoogleCredential.FromStream(stream.ReadToEnd()).CreateScoped(_scopes);
            return _credential;
        }

我收到一个错误

错误 CS1503 参数 1:无法从“字符串”转换为“System.IO.Stream”

虽然,在使用类似的 FileStream 时,没有这样的错误:

var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))

_credential = GoogleCredential.FromStream(stream).CreateScoped(_scopes);

如果您不创建流,代码也可以工作,而只需指定 FromFile 方法并直接指定 json 文件(据我了解,在这种情况下,流是在方法内部创建的?):

_credential = GoogleCredential.FromFile("client_secrets.json").CreateScoped(_scopes);
c#
  • 0 个回答
  • 0 Views
Martin Hope
Humble Newbie
Asked: 2022-08-26 20:22:48 +0000 UTC

#C 除法递归或将 int 转换为字符串的递归

  • 1

我为这个任务做了两种方法,它们做同样的事情(它们计算数字 int 的数字之和)。

第一个 - 最合乎逻辑的(在外观上) - 在制定了第二种方法并刷新了学校数学课程的负责人之后浮现在脑海中:

int SumOfNumber(int number)
{
    if(number == 0)
        return 0;
    return number % 10 + SumOfNumber(number / 10);
}

第二种方法 - 最初想到的,但是,与第一种方法相比,有更多的动作 - 首先我将 int 转换为字符串,然后我在原始方法中使用递归创建了一个嵌套方法,其中我将字符串元素转换回进入int。

int SumOfNumber1(int number)
{
    string? temp = Convert.ToString(number);
    return SumOfNumber2(temp);

    int SumOfNumber2(string temp, int i = 0)
    {
        if (i >= temp.Length)
            return 0;
        return Convert.ToInt32(temp[i].ToString()) + SumOfNumber2(temp, i + 1);
    }
}

在那之后,我决定找出哪种方法对处理器来说更昂贵。谷歌搜索,除法运算对于处理器来说是相当昂贵的。事实证明,第二种方法更适合使用,尽管自行车具有 int 转换和堆栈上的大量调用?

c#
  • 1 个回答
  • 37 Views
Martin Hope
Humble Newbie
Asked: 2022-08-22 19:20:07 +0000 UTC

c# 来自枚举的输出数据因枚举中的元素值类型而异(偶数/奇数)

  • 0
enum Days
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday = 2,
    Saturday
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Days.Sunday);
        Console.WriteLine(Days.Monday);
        Console.WriteLine(Days.Tuesday);
        Console.WriteLine(Days.Wednesday);
        Console.WriteLine(Days.Thursday);
        Console.WriteLine(Days.Friday);
        Console.WriteLine(Days.Saturday);
    }
}

我不明白为什么,如果给 Friday 元素分配了一个没有余数 (0, 2, 4) 的数字,那么具有相同数字的元素将被替换为 Friday,如果有余数 (1, 3),那么星期五本身被具有相同数字的元素替换。

例如:

Friday = 2 Console.WriteLine(Days.Tuesday) 是星期五;

Friday = 1 Console.WriteLine(Days.Friday) 是星期一;

它是枚举的一个特性吗?

c# enum
  • 1 个回答
  • 39 Views
Martin Hope
Humble Newbie
Asked: 2022-08-15 23:56:51 +0000 UTC

最佳方法数量(1 个无效或 3 个返回)?

  • 0

有一个 void 方法,在它的主体中:

  1. 对第一次出现的二进制搜索
  2. 最后一次出现的二进制搜索
  3. 将所有索引写入字符串并输出到控制台的循环

感觉方法太重了:(

问题:创建方法有没有好的礼仪?例如,我可以制作 3 种方法而不是 1 种:

  1. int 第一次出现
  2. int 最后一次出现
  3. 带索引的字符串循环

它会更加简洁,但是方法的数量从 1 增加到 3。

一个大方法是否应该总是分解成更简单的方法?

还是纯粹是个人喜好问题,这里没有礼貌?

多合一方法

void IndexFinderArrayBS(int number, int[] array)
{
    int low = 0;
    int high = array.Length - 1;
    int resultfirst = -1;
    int resultlast = -1;

    while (low <= high)
    {
        int mid = (low + high) >> 1;

        if (array[mid] > number)
        {
            high = mid - 1;
        }
        else if (array[mid] < number)
        {
            low = mid + 1;
        }
        else
        {
            resultfirst = mid;
            high = mid - 1;
        }
    }

    low = 0;
    high = array.Length - 1;

    while (low <= high)
    {
        int mid = (low + high) >> 1;

        if (array[mid] > number)
        {
            high = mid - 1;
        }
        else if (array[mid] < number)
        {
            low = mid + 1;
        }
        else
        {
            resultlast = mid;
            low = mid + 1;
        }
    }

    string? indexes = "";
    int counter = 0;

    for (int i = resultfirst; i <= resultlast; i++)
    {
        counter++;
        if (i == resultfirst)
        {
            indexes += Convert.ToString($"{i}");
        }
        else
        {
            indexes += Convert.ToString($", {i}");
        }
    }

    indexes += ".";

    if (resultfirst == -1 && resultlast == -1)
    {
        Console.WriteLine($"\nNumber {number} was not found in the Array");
    }
    else if (resultfirst == resultlast)
    {
        Console.WriteLine($"Number {number} was found only 1 time in the array. Index is: {resultfirst}");
    }
    else
    {
        Console.WriteLine($"\nNumber {number} was found {counter} times in the array. Indexes are: {indexes}");
    }
}

具有两个嵌套 int 方法的字符串方法

string IndexFinderArrayBS(int number, int[] array)
{
    int resultfirst = IndexFinderArrayBSFirst(number, array);
    int resultlast = IndexFinderArrayBSLast(number, array);

    string? indexes = "";
    int counter = 0;

    for (int i = resultfirst; i <= resultlast; i++)
    {
        counter++;
        if (i == resultfirst)
        {
            indexes += Convert.ToString($"{i}");
        }
        else
        {
            indexes += Convert.ToString($", {i}");
        }
    }

    indexes += ".";

    if (resultfirst == -1 && resultlast == -1)
    {
        return string.Format("\nNumber {0} was not found in the Array", number);
    }
    else if (resultfirst == resultlast)
    {
        return string.Format("\nNumber {0} was found only 1 time in the array. Index is: {1}", number, resultfirst);
    }
    else
    {
        return string.Format("\nNumber {0} was found {1} times in the array. Indexes are: {2}", number, counter, indexes);
    }
}

和 int 方法之一

int IndexFinderArrayBSFirst(int number, int[] array)
{
    int low = 0;
    int high = array.Length - 1;
    int result = -1;

    while (low <= high)
    {
        int mid = (low + high) >> 1;

        if (array[mid] > number)
        {
            high = mid - 1;
        }
        else if (array[mid] < number)
        {
            low = mid + 1;
        }
        else
        {
            result = mid;
            high = mid - 1;
        }
    }
    return result;
}
c# методы
  • 1 个回答
  • 56 Views
Martin Hope
Humble Newbie
Asked: 2022-08-11 21:37:55 +0000 UTC

C# - 关于在 Microsoft 示例中使用 Trim() 的问题

  • 0

示例代码

using System;

public class Example
{
    public static void Main()
    {
        Console.Write("Enter your first name: ");
        string firstName = Console.ReadLine();
      
        Console.Write("Enter your middle name or initial: ");
        string middleName = Console.ReadLine();
      
        Console.Write("Enter your last name: ");
        string lastName = Console.ReadLine();
      
        Console.WriteLine();
        Console.WriteLine("You entered '{0}', '{1}', and '{2}'.", 
                        firstName, middleName, lastName);
      
        string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " + 
                    lastName.Trim()).Trim();
        Console.WriteLine("The result is " + name + ".");

        // The following is a possible output from this example:
        //       Enter your first name:    John
        //       Enter your middle name or initial:
        //       Enter your last name:    Doe
        //       
        //       You entered '   John  ', '', and '   Doe'.
        //       The result is John Doe.
    }
}

问题——为什么不是用户从键盘输入的变量名,在用户自己输入的变量截断后,还是截断了?

字符串名称 = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " + lastName.Trim()) .Trim() ;

该代码无需此添加即可工作。

c# microsoft
  • 1 个回答
  • 46 Views
Martin Hope
Humble Newbie
Asked: 2022-08-11 00:10:53 +0000 UTC

C#。新手无法弄清楚如何反转数组数组[关闭]

  • -1
关闭。这个问题需要澄清或补充细节。目前不接受回复。

想改进这个问题?通过编辑此帖子添加更多详细信息并澄清问题。

1 个月前关闭。

改进问题

从零开始学习 C#,没有任何编程经验。已到达数组(提前 - 方法)。通过按类型轻松排序,弄清楚如何对常规数组(一维、二维、三维等)进行排序和反转

temp = nums[i][j];
nums[i][j] = nums[k][m];
nums[k][m] = temp;

但是我无法理解如何通过类似的构造来反转数组数组:((

我知道我正在重新发明轮子,但我想利用目前可用的知识来做这件事,并在几个月后回到这个任务并正常做。

一些特定的数组数组,如

int[][] nums = new int[3][];
nums[0] = new int[1];
nums[1] = new int[2];
nums[2] = new int[3];

事实证明,通过拐杖可以完成,但这是一种一次性解决方案,如果您更改数组元素的数量,它就不起作用。

数组数组的降序和升序排序没有问题,一切都不需要拐杖,堵嘴只有反向。

这是一个反转二维数组的示例(不笑,只是我尝试这样做的一个示例)

Random random = new Random();

int[,] numbers = new int[random.Next(2,10), random.Next(2, 10)];

for (int i = 0; i < numbers.GetLength(0); i++)
{
    for (int j = 0; j < numbers.GetLength(1); j++)
    {
        numbers[i, j] = random.Next(-100, 100);
    }
}

Console.WriteLine($"2D array is: {numbers.GetLength(0)}, {numbers.GetLength(1)}");
Console.WriteLine();
Console.WriteLine("Original array is: ");

for (int i = 0; i < numbers.GetLength(0); i++)
{
    for (int j = 0; j < numbers.GetLength(1); j++)
    {
        Console.Write($"{numbers[i, j]}\t");
    }
    Console.WriteLine();
}
Console.WriteLine();

int temp = 0;
int i_mid = numbers.GetLength(0) / 2;
int j_mid = numbers.GetLength(1) / 2;                          

for (int i = 0; i < numbers.GetLength(0); i++)
{
    for (int j = 0; j < j_mid; j++)
    {
        temp = numbers[i, j];
        numbers[i, j] = numbers[numbers.GetLength(0) - (i + 1), numbers.GetLength(1) - (j + 1)];
        numbers[numbers.GetLength(0) - (i + 1), numbers.GetLength(1) - (j + 1)] = temp;
    }
    if (remainder > 0 && i < i_mid)
    {
        temp = numbers[i, j_mid];
        numbers[i, j_mid] = numbers[numbers.GetLength(0) - (i + 1), j_mid];
        numbers[numbers.GetLength(0) - (i + 1), j_mid] = temp;
    }
}
Console.WriteLine("Reversed array is: ");

for (int i = 0; i < numbers.GetLength(0); i++)
{
    for (int j = 0; j < numbers.GetLength(1); j++)
    {
        Console.Write($"{numbers[i, j]}\t");
    }
    Console.WriteLine();
}
Console.WriteLine();

并对数组数组进行排序

Random random = new Random();
int temp = 0;
int[][] nums = new int[3][];
nums[0] = new int[1];
nums[1] = new int[2];
nums[2] = new int[3];

Console.WriteLine("Original array is: ");
for (int i = 0; i < nums.Length; i++)
{
    for (int j = 0; j < nums[i].Length; j++)
    {
        nums[i][j] = random.Next(100);
    }
}

for (int i = 0; i < nums.Length; i++)
{
    for (int j = 0; j < nums[i].Length; j++)
    {
        Console.Write($"{nums[i][j]}\t");
    }
    Console.WriteLine();
}

for (int i = 0; i < nums.Length; i++)
{
    for (int j = 0; j < nums[i].Length; j++)
    {
        for (int k = 0; k < nums.Length; k++)
        {
            for (int m = 0; m < nums[k].Length; m++)
            {
                if(nums[i][j] > nums[k][m])
                {
                    temp = nums[i][j];
                    nums[i][j] = nums[k][m];
                    nums[k][m] = temp;
                }
            }
        }
    }
}
Console.WriteLine();
Console.WriteLine("Descending sorted array is: ");

for (int i = 0; i < nums.Length; i++)
{
    for (int j = 0; j < nums[i].Length; j++)
    {
        Console.Write($"{nums[i][j]}\t");
    }
    Console.WriteLine();
}
c# массивы
  • 1 个回答
  • 93 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5