小编Ibr*_*mir的帖子

在适合C#性能的方法中使用'ref'关键字作为字符串参数?

作为一个对.NET管道不太了解的程序员,我想知道使用ref字符串作为参数是否有利于C#的性能?

假设我有一个像这样的方法:

public int FindSomething(string text)
{
    //Finds a char in the text and returns its index
}
Run Code Online (Sandbox Code Playgroud)

当我使用这个方法时,编译器会为方法创建一个文本副本,对吧?

但是,如果我使用ref关键字:

public int FindSomething(ref string text)
{
    //Finds a char in the text and returns its index
}
Run Code Online (Sandbox Code Playgroud)

..编译器应该只发送文本的指针地址...

那么使用ref这样的性能有好处吗?

c# string performance ref

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

static和const变量之间的区别

在声明全局变量时,"static"和"const"之间有什么区别;

namespace General
{
    public static class Globals
    {
        public const double GMinimum = 1e-1;

        public const double GMaximum = 1e+1;
    }
}
Run Code Online (Sandbox Code Playgroud)

哪一个更好(考虑到这些变量永远不会改变)

namespace General
{
    public static class Globals
    {
        public static double GMinimum1 = 1e-1;

        public static double GMaximum1 = 1e+1;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# variables static global const

10
推荐指数
3
解决办法
4万
查看次数

在编译时禁用字段定义

我想创建一个有/没有线程安全的池。我不想定义互斥体字段,如果池不是线程安全的,所以我使用了 std::conditional,但是因为它没有完全按照我想要的方式进行操作,并且创建了两个“类型”选项,所以我选择了“int8 (char)” “作为钝化互斥体类型。(相反,我希望整个定义消失)

template<typename T, bool threadSafe = true>
class Pool
{
private:
    //Mutex mutex; this is the field i want it to be DISAPPEARED, i modified it as below
    std::conditional<threadSafe, Mutex, int8>::type mutex;
protected:
    static constexpr item_type_size_datatype TypeSizeX = sizeof(T) + sizeof(size_t);
public:
    Pool(size_t clusterItemCount) : ClusterItemCount(clusterItemCount),
        ClusterByteSize(clusterItemCount* TypeSizeX)
    {
#ifdef CriticalSection
        if constexpr (threadSafe)
            InitializeCriticalSection(&mutex);
#endif
    }
    ~Pool()
    {
        Clear();

#ifdef CriticalSection
        if constexpr (threadSafe)
            DeleteCriticalSection(&mutex);
#endif
    }

    T* Occupy(bool& outFirstTime)
    {
        if constexpr (threadSafe)
        {
            MutexLock(mutex); …
Run Code Online (Sandbox Code Playgroud)

c++

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

我是否必须继续创建Graphics对象

我是一个老的delphi程序员,我习惯于创建对象并将它们全部用于有效的内存使用.但是在c#(也许是我见过的所有教程)中,你new每次都在创建东西(感谢垃圾收集器!!,让我做编码)..

无论如何,我正在尝试创建一个有很多绘图的设计软件.我的问题是:我是否必须创建一个图形对象,或者使用protected override void OnPaint(PaintEventArgs e)e.Graphics每个绘画事件..因为当我创建一个图形对象然后调整我绘制的控件时,我创建的图形对象具有剪切问题,只绘制旧的矩形区域..

谢谢

c# gdi+ clipping onpaint winforms

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

泛型字典类是否有通过索引获取键的方法?

这可能是一个非常简单的问题,但我认为我是故意盲目的东西; 如何通过Delphi(10.1)中的Dictionary类中的索引获取密钥.我的意思是结构有一个名为Count的属性,所以它必须有一些数组或列表,为什么我不能通过索引得到键.

我也在Dictionary类中尝试过KeyCollection属性,但它也没有任何用处.我需要这样的东西:

key: string;
key := dicTest.GetKey(keyIndex);
Run Code Online (Sandbox Code Playgroud)

非常感谢.

delphi dictionary

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

标签 统计

c# ×3

c++ ×1

clipping ×1

const ×1

delphi ×1

dictionary ×1

gdi+ ×1

global ×1

onpaint ×1

performance ×1

ref ×1

static ×1

string ×1

variables ×1

winforms ×1