小编Met*_*Man的帖子

初学者试图使用其他陈述C#?

我是一名学生第一次参加计算机课程,我一直试图"破解"这个课程多年(我对此很新......抱歉!).我似乎无法构建一个简单的if/else语句,如果满足条件,它会写出一个肯定的响应,如果不满足,则会写一个否定的响应.但是我得到了错误:无效的表达式术语'else'
只有赋值,调用,递增,递减和新对象表达式才能用作语句

对不起,如果这没有意义,我很困惑!:(

static void Main(string[] args)
{
    Console.WriteLine("Do you like bananas?");
    Console.ReadLine();
    if (Console.ReadLine() == "Yes") ;
    {
        Console.WriteLine("Thanks for your time!");
    }
    else;
    {
        Console.WriteLine("Oh okay.");
    }
 }
}
Run Code Online (Sandbox Code Playgroud)

}

c# if-statement

1
推荐指数
1
解决办法
150
查看次数

继承C#到VB的位置

有人可以解释一下下面这个功能的含义吗?对不起,我是VB开发人员,我无法理解下面的语法.转换器工具将其无意义地转换为VB代码.

  • 我不明白T的用法
  • 我不理解Where T的继承

     public static string ValidateSeName<T>(this T entity, string seName, string name, bool ensureNotEmpty)
             where T : BaseEntity, ISlugSupported
    {
    }
    
    Run Code Online (Sandbox Code Playgroud)

调用此函数的位置,调用函数如下所示.这个T实体的第一个参数发生了什么,它不用作参数?它的目的是什么?

  model.SeName = category.ValidateSeName(model.SeName, category.Name, true);
Run Code Online (Sandbox Code Playgroud)

如果你能在VB中给我相同的代码,将会很有帮助.谢谢

c# vb.net c#-to-vb.net

1
推荐指数
1
解决办法
112
查看次数

有没有办法缩短在赋值中使用if语句?

例如,如果我有这个代码:

if (foo != default(foo))
{ 
  int foo2 = foo; 
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将此缩短为仅作业?在伪代码中,类似于:foo2 =如果不是默认的foo

c#

0
推荐指数
1
解决办法
89
查看次数

异常说堆栈是空的,当它不是?

advanced collections在书中学到了一些等等,并且遇到了stacks.我得到了概念,但想制作一个快速程序,从defined point in the stack当时删除一个项目places all the values back onto the stack.我在这里有我的代码,但我得到System.InvalidOperationException类型的异常,其中堆栈的其他信息为空.我似乎无法理解; 有人可以帮忙吗?

这是我的代码:

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

namespace StackRemover
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 0; //the index they want to remove
            Stack[] array = new Stack[1]; // will hold the array returned by remove()
            Stack stack = new Stack();

            //fill the stack with values from …
Run Code Online (Sandbox Code Playgroud)

.net c# stack

0
推荐指数
1
解决办法
800
查看次数

Lambda表达式中的Select语句出错

我有以下代码:

getAllResult.GroupBy(g => g.OriginatingTransactionID)
.Select(r => 
{
    usp_GetAll_Result getAllResult1 = r.Select(x => x).FirstOrDefault();
    Bundle bundle = new Bundle
    {
        BundleName = getAllResult1.BundleName,
        BundleStatusCode = getAllResult1.BundleStatusCode,
        BundleStatusReasonCode = getAllResult1.BundleStatusReasonCode
    };
}).ToList();
Run Code Online (Sandbox Code Playgroud)

编译时我收到错误:

无法从用法推断出方法'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,System.Func)'的类型参数.尝试显式指定类型参数.

c# linq

0
推荐指数
1
解决办法
62
查看次数

如何从C#中的VisualBasic输入框中获得答案

我试图在C#中创建一个InputBox,但我已经设法弄清楚这是怎么做的,现在我无法提取答案.每当我尝试提取答案时,它都会出现以下错误:" 无法将类型'字符串'隐式转换为System.Windows.Forms.Label'." 我到目前为止编写的代码如下.

private void devBtn1_Click(object sender, EventArgs e)
{
    string bluePlyr1;
    bluePlyr1 = Microsoft.VisualBasic.Interaction.InputBox("Name of Player 1");
    devLbl1 = bluePlyr1;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c#

0
推荐指数
1
解决办法
43
查看次数

C#如何返回上一个语句

您好我想知道如何返回C#中的上一个语句

例如,我想在填写错误时再次显示用户readline,当他正确执行时,它会向他显示下一行代码/语句(在此考试中.Console.WriteLine("Hi"))

我怎么能这样做?

int num;
string prvnicislo;
prvnicislo = Console.ReadLine();

while (true)
{
    if (int.TryParse(prvnicislo, out num))
    {
        Convert.ToInt32(prvnicislo);
    }
    else
    {
        Console.WriteLine("'{0}' is not int, try it again:", prvnicislo);
        prvnicislo = Console.ReadLine();
    }
}
Console.WriteLine("Hi");
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
328
查看次数

将两个目录与​​文件进行比较,然后在另一个文件中给出结果差异.C#

我将两个目录与​​其中的文件进行比较.一个是旧目录(旧),另一个是新目录(New),其中包含更多文件.我能够打印输出显示所有添加的文件,但它只显示结果文件中我真正希望它去的一个文件.如何让它显示结果文件中添加的所有文件?

Visual Studio上的输出

在此输入图像描述

结果文件中的输出

在此输入图像描述

这是我的代码:

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{      
    String New = @"C:\Compare\New";
    String Old = @"C:\Compare\Old";
    String Result = @"C:\Compare\Result";
    DirectoryInfo dir1 = new DirectoryInfo(New);
    DirectoryInfo dir2 = new DirectoryInfo(Old);

    // Take a look of the file system.  
    IEnumerable<FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.AllDirectories);
    IEnumerable<FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.AllDirectories);

    //A custom file comparer defined below  
    FileCompare compare = new FileCompare();

    // Check if same or not.
    // bool because of FileCompare
    bool …
Run Code Online (Sandbox Code Playgroud)

c# compare visual-studio

0
推荐指数
1
解决办法
672
查看次数

尝试对文本框求平方根时无法将类型'string'隐式转换为'System.Windows.Controls.TextBox'错误

我试图平方根文本框值并将其输出到另一个文本框。这是我到目前为止所拥有的:

private void ButtonSquareRoot_Click(object sender, RoutedEventArgs e)
{
    double Input1double = Convert.ToDouble(TextBoxInput1.Text);
    double VarTextBoxAnswer = Math.Sqrt(Input1double);
    TextBoxAnswer = VarTextBoxAnswer.ToString();
}
Run Code Online (Sandbox Code Playgroud)

它会显示以下错误消息:

 Severity   Code    Description Project File    Line    Suppression State
Run Code Online (Sandbox Code Playgroud)

错误CS0029无法将类型'string'隐式转换为'System.Windows.Controls.TextBox'计算器c:\ users \ user \ documents \ visual studio 2017 \ Projects \ calculator \ calculator \ MainWindow.xaml.cs 125有效

第125行是从TextBoxAnswer开始的行

如果您有任何解决方法的想法,请帮忙!如果我还不够具体,请回复,我将提供更多信息。

c#

0
推荐指数
1
解决办法
1724
查看次数

使用ASP.NET C#中的客户端ID和客户端密钥访问Sharepoint列表

目前,我可以使用用户ID和密码访问共享点列表,如下所示。但是,我想了解如何使用客户端ID和客户端密钥访问列表?

string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
ClientContext clientContext = new ClientContext(siteUrl);
string username = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
System.Security.SecureString passWord = new System.Security.SecureString();
foreach (char c in password.ToCharArray()) 
{    
    passWord.AppendChar(c);
}

clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
Run Code Online (Sandbox Code Playgroud)

c# asp.net sharepoint

0
推荐指数
1
解决办法
3616
查看次数