小编Dan*_*ley的帖子

在c#中尝试将节点添加到队列时不能使用.Next

尝试在C#中添加和删除队列中的节点.但是,当尝试使用时,front.Next我得到一个编译器错误,该定义未包含在内.

class Queue<T>
{
    int count = 0;
    Node<T> front = null;
    Node<T> end = null;

    public void Enqueue(T obj)
    {
        if (count == 0)
        {
            front = new Node<T>(obj);
        }
        else 
        {
            Node<T> newEnd = new Node<T>(obj);
            newEnd.Next = end;
            count++;
            end = newEnd;
        }
    }

    public T Dequeue(T obj)
    {
        Node<T> newFront = new Node<T>(obj);
        newFront = front.Next;
        count--;
        front = newFront;
        return front;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# queue

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

框中的颜色,c#控制台应用程序

我正在创建一个Bordered Box,它应该在边框处创建一个具有不同颜色的框.这是我的代码:

class BorderedBox : ColoredBox
{
    public int heigth;
    public int width;

    ConsoleColor color = borderColor;

    public BorderedBox (Point p, int width, int height, ConsoleColor backColor, ConsoleColor borderColor)
        : base (p, width, height, backColor)
    {
            this.borderColor = borderColor;
    }
    public override void Draw()
    {
        for (int j = 0; j < height; j++)
        {
            Console.SetCursorPosition(p.X, p.Y + j);

            for (int i = 0; i < width; i++)
            {   
                if (i == 0 || i == width - 1 || …
Run Code Online (Sandbox Code Playgroud)

c#

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

用C#从字符串中提取前两个单词

我们正在尝试锻炼C#代码从字符串中提取前两个单词.下面是我正在做的代码.

public static string GetDetailsAsString(string Details)
    {


        string Items = //how to get first 2 word from string??? 

       if (Items == null || Items.Length == 0)
           return string.Empty;
       else
           return Items;
    }
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
2
解决办法
9109
查看次数

构造函数参数

我正在通过一个例子,并发现了以下内容:

public CustomerDetailsViewModel(
    IUIDataProvider dataProvider,
    string customerID,
    IToolManager toolManager = null)
    : base(toolManager)
{
    _dataProvider = dataProvider;
    Customer = _dataProvider.GetCustomer(customerID);
    Customer.PropertyChanged += Customer_PropertyChanged;
    DisplayName = Customer.CompanyName;
Run Code Online (Sandbox Code Playgroud)

有人可以更详细地解释以下部分,因为我不确定它是如何使用的:

 IToolManager toolManager = null)
 : base(toolManager)
Run Code Online (Sandbox Code Playgroud)

c#

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

在查询中使用单引号保存数据的问题

我有一个asp.net项目,我在保存数据时遇到错误.我有一个学校名称的文本框,大多数学校名称都被保存但如果我有学校名称像St.Xavier's,它会产生一个问题执行插入查询.这是因为文本中存在单引号.因此,我得到的最终查询是:

Insert Into tblSchool(SchoolName, CreatedBy, CreatedOn) 
values('St.Xavier's',1,'2014-6-13     13:14:16')
Run Code Online (Sandbox Code Playgroud)

如何在文本中使用单引号保存数据?我正在使用Microsoft SQL Server 2005.

c# sql sql-server asp.net

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

Task.Wait在MVC5中锁定控制器的流程

我们正在从MVC站点使用WebAPI.我们从这样的控制台应用程序中做了一个概念验证,使用了AddUser Method,它在我们的数据库中调节了一个新用户.

static void Main()
    {
        M_Users user = new M_Users();

        var newUser = new M_Users()
        {
            Password = "123",
            FirstName = "Andrew",
            Email = "someAndrew@someplace.com",
            AdminCode = 1,
            IsDisabled = false,
            CountryId = 48,
            CityId = 149,
            DepartmentId = 3,
            CreationDate = DateTime.Now,
            CreatorUserId = 1
        };
        var star = RunAsync(newUser);
        star.Wait();

        //THIS LINE IS REACHED ALWAYS
        Console.WriteLine(star.Result.UserID);
        Console.ReadLine();
    }

    static async Task<M_Users> AddUserAsync(M_Users newUser)
    {
        M_Users user = new M_Users();
        string requestUri = "api/User/AddUser";

        using (var client = …
Run Code Online (Sandbox Code Playgroud)

c# async-await asp.net-web-api asp.net-mvc-5

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

枚举时数组对象为空,但通过索引访问时不为空

当我通过HttpClient对象从web api获取数据时,我有异步方法.我打印出响应字符串,我可以看到我有正确的数据.

我还将响应转换为我的自定义类的数组,它的工作正常(我可以将属性记录到控制台).

当我通过索引打印属性时一切正常:

Debug.WriteLine("B: " + brands[0].Brand);
Run Code Online (Sandbox Code Playgroud)

但是当我枚举数组时它没有记录数据,看起来它包含空字符串,为什么?:

foreach (Brand brand in brands)
{
    Debug.WriteLine("BRAND: ", brand.Brand.ToString()); // Prints just BRAND:
}
Run Code Online (Sandbox Code Playgroud)

这是我的方法的样子:

 public static async Task<Brand[]> LoadBrandsAsync()
    {
        Brand[] brands = null;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(serviceUrl);
            HttpResponseMessage response = await client.GetAsync("Brands");
            try
            {
                brands = await response.Content.ReadAsAsync<Brand[]>();
                Debug.WriteLine("Count: " + brands.Count());
                Debug.WriteLine("B: " + brands[0].Brand); // This works fine 
                Debug.WriteLine("B: " + brands[0].Sales); // it print out the data

                foreach (Brand …
Run Code Online (Sandbox Code Playgroud)

c# async-await

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

什么时候使用Task.Run浪费或妄想?

我有一个读取/写入对象到存储的接口.在一种情况下,存储是具有异步方法的数据库.在另一种情况下,它只是一个cookie.

我认为它建议在异步调用结束时沿着路径使用异步,因此看起来有意义的接口也是异步的.但是在cookie的情况下,我只是设置几个字段并将其粘贴在响应中,因此还没有任何异步.我可以在等待Task.Run()来匹配新接口,但我不知道这是否可取或是否对性能有负面影响.

该怎么办?

public interface IProfileStore
{
    async Task SetProfile(UserProfile profile);
}

public async Task SetProfile(UserProfile profile)
{
    // Look mom, I'm needlessly async
    await Task.Run(() =>
    {
        var cookie = new HttpCookie(AnonymousCookieName);
        cookie["name"] = profile.FullName;
        HttpContext.Current.Response.Cookies.Add(cookie);
    });
}
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await

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

对 C# 字典中值的属性求和

我有这样的东西

Dictionary<String, Profile > sto;
Run Code Online (Sandbox Code Playgroud)

其中 Profile 是具有不同属性的类(例如 int Ints)。现在我想知道是否有一个命令可以对整个字典中的所有整数求和。

c# dictionary

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

返回主码

我刚刚在C#中编写了一个简单的代码,用于显示一条消息,该消息将您的输入视为睡眠时间,并根据该消息返回您是否休息好或不休息.

问题是无论何时键入除整数之外的任何内容都会引发异常,因此我尝试使用try和catch方法处理此问题.我希望我的代码在下次正确输入整数后再次返回评估.如何修改我的代码来执行此操作?

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("your name");
            string name = Console.ReadLine();
            Console.WriteLine("how many hours of sleep did you get");

            try
            {
                int hoursOfSleep = int.Parse(Console.ReadLine());
                Console.WriteLine("hello " + name);

                if (hoursOfSleep > 8)
                {
                    Console.WriteLine("you are well rested");
                }
                else
                {
                    Console.WriteLine("you need sleep");
                }
            }
            catch
            {
                Console.WriteLine("Invalid Hours !! ");
                Console.WriteLine(" Enter hours in integer");
            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c#

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

同步执行异步功能

我已经对这个主题进行了大量的搜索,我在这个网站上阅读了关于这个主题的大部分帖子,但是我仍然感到困惑,我需要一个直接的答案.这是我的情况:

我有一个已建立的Winform应用程序,我无法将其全部"异步".我现在被迫使用一个全部写为异步函数的外部库.

在我的申请中,我有

/// <summary>
/// This function I can't change it to an 'async'
/// </summary>
public void MySyncFunction()
{
    //This function is my point in my application where I have to call the
    //other 'async' functions but I can't change the function itself to 'async'

    try
    {
        //I need to call the MyAsyncDriverFunction() as if it is a synchronous function
        //I need the driver function to finish execution and return before processing the code that follows it
        //I …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous winforms async-await .net-4.5

-2
推荐指数
1
解决办法
147
查看次数

SQL分组依据为NULL

我有一张桌子,像这样:

ID   ProductID   ProductName  Price
==   =========   ===========  =====
1    XX1           TShirt      10
2    XX1           TShirt      10
3    NULL          TShirt      10
4    XX2           Shirt       20
5    XX3           Shirt1      30
Run Code Online (Sandbox Code Playgroud)

现在我希望以此为依据ProductName,结果将如下

ID   ProductID   ProductName  Price
==   =========   ===========  =====
1    XX1           TShirt      30
4    XX2           Shirt       20
5    XX3           Shirt1      30
Run Code Online (Sandbox Code Playgroud)

谢谢

sql null

-4
推荐指数
1
解决办法
53
查看次数