相关疑难解决方法(0)

C#=意思是2个不同的东西

我怀疑这背后有一些非常重要的东西,我完全没有注意到.我可以写

int b = 5;
int a = b;
a = 2; 
Run Code Online (Sandbox Code Playgroud)

从我能说的最好,这给了我两个独立的变量.最初,a设置为5,但是我可以在不改变b的情况下将a更改为2

但是,我可以写

double[] b = { 1, 2, 3, 4};
double[] a = b; 
a[2] = 9; 
Run Code Online (Sandbox Code Playgroud)

现在看来,我没有2个独立的变量,而是对同一个实体有2个引用.现在改变a [2]会改变b [2].这是怎么回事?

c#

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

Vector3.Set 和 Rigidbody2D.velocity.Set 和功能无法正常工作

bodyis的简单移动脚本中gameObject.GetComponent<Rigidbody2D> (),此代码应用移动

body.velocity = new Vector2 (10 * dir, body.velocity.y);
Run Code Online (Sandbox Code Playgroud)

然而,在这一个没有运动(也没有错误)出现

body.velocity.Set (10 * dir, body.velocity.y);
Run Code Online (Sandbox Code Playgroud)

我在Unity 文档中没有看到任何解释。你能解释为什么body.velocity.x在第二种情况下仍然为零吗?

c# properties unity-game-engine

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

代码正在修改错误的变量......为什么?

我有一个奇怪的事情,我正在做的一些代码是修改副本和原始列表..我已尽可能地解决问题,只能在单个文件中显示错误.虽然我的现实世界的例子我们要复杂得多......但是根本就是这个问题.

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

namespace TestingRandomShit
{
    class Program
    {
        private static string rawInput;
        private static List<string> rawList;
        private static List<string> modifiedList;

        static void Main(string[] args)
        {
            rawInput = "this is a listing of cats";

            rawList = new List<string>();
            rawList.Add("this");
            rawList.Add("is");
            rawList.Add("a");
            rawList.Add("listing");
            rawList.Add("of");
            rawList.Add("cats");

            PrintAll();

            modifiedList = ModIt(rawList);

            Console.WriteLine("\n\n**** Mod List Code has been run **** \n\n");
            PrintAll();
        }

        public static List<string> ModIt(List<string> wordlist)
        {

            List<string> huh = new List<string>();
            huh = …
Run Code Online (Sandbox Code Playgroud)

c#

3
推荐指数
2
解决办法
163
查看次数

当数据类型为 int 时,值 '' 无效

我正在尝试在表单验证中显示友好的错误消息。我在模型类中有一个带有注释的属性:

[Required(ErrorMessage="The number attribute is required")]
public int Level { get; set; }
Run Code Online (Sandbox Code Playgroud)

它不起作用,但是当我将数据类型更改为 时string,会显示注释的错误消息。这是否意味着int不支持?

asp.net-mvc

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

在 Update() 方法中创建新对象

我在某处读到我不应该在游戏引擎的 Update() 方法中创建任何新对象实例,因为这会导致唤醒垃圾收集器并降低性能,但有时我在一些教程中看到他们使用 new 关键字unity Update() 方法!这个可以吗?团结会以某种方式处理这个问题,还是不?

c# garbage-collection unity-game-engine unity5

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

C#对象和内存管理

假设我有以下代码,其中Car类只有1个属性: String modelname

Car c = new Car("toyota");
Car c1 = c;
Car c2 = c;
Car c3 = c;
Car c4 = c;
Car c5 = c;
Run Code Online (Sandbox Code Playgroud)

这是否每次都会制作一张新车?所以在记忆中会有一个新的"丰田"字符串5倍以上?或者"丰田"字符串只会在内存中一次?

编辑:添加此相关链接,以防您遇到与我相同的问题,我认为这有助于 数组或列表默认通过c#中的引用传递?

c# object

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

没有out或ref的方法如何在外部改变变量?C#

如何aSumElemtnts方法内部更改数组的值?是因为它是静态方法吗?因为该方法仅返回变量,sum并且没有ref或out.

class Program
{
    public static int SumElements(int[] a)
    {
        int[] a2 = a;
        int sum = 0;
        for (int i = 0; i < a2.Length; i++)
        {
            int temp = a2[i] * 10;
            a2[i] = temp;
            sum += temp;
        }
        a[a2.Length - 2] = sum;
        return sum;
    }

    public static void Main(string[] args)
    {
        int[] a = { 1, 2, 3 };
        int sum = SumElements(a);

        Console.Write("Sum = " + sum + ", …
Run Code Online (Sandbox Code Playgroud)

c# pass-by-reference pass-by-value

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

列表中的数组双变化值c#

当我重新计算用于输入列表中的值的变量时,我无法理解为什么列表的值会发生变化.

看一个例子.

List<double[]> myList = new List<double[]>();
double[] a = new double[3];

a[0] = 1;
a[1] = 2;
a[2] = 3;
myList.Add(a); // Ok List[1] = 1 2 3

a[0] = 4;      // List[1] = 4 2 3
a[1] = 5;      // List[1] = 4 5 3
a[2] = 6;      // List[1] = 4 5 6
myList.Add(a); // List[1] = 4 5 6 and List[2] = 4 5 6
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?

.net c#

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

将字典添加到字典列表

我有如下代码:

static void Main(string[] args)
{
    List<Dictionary<string, string>> abc = new List<Dictionary<string, string>>();

    Dictionary<string, string> xyz = new Dictionary<string, string>();
    Dictionary<string, string> klm = new Dictionary<string, string>();

    xyz.Add("xyz_1", "4");
    xyz.Add("xyz_2", "5");

    klm.Add("klm_1", "9");
    klm.Add("klm_2", "8");

    abc.Add(xyz);
    xyz.Clear();
    /*after this code xyz clear in my abc list. Why ?*/
    abc.Add(klm);
}
Run Code Online (Sandbox Code Playgroud)

我创建字典列表。然后,我将字典添加到此列表中。但是添加字典后,具有清除功能的字典将为空,而不是从列表中删除。通常,我的“ abc”列表应包含xyz和klm字典及其值。但是运行clearfunction时xyz值计数将为0。我该如何预防该站?

c# dictionary list add

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

什么时候传递给方法的参数值被复制到方法中?这种行为是否低于预期?

下面是一些非常简单的代码,将突出我的问题/问题

static void Main(string[] args)
{
    var myInitialString = "My Initial String";
    Console.WriteLine($"{nameof(myInitialString)}: {myInitialString}");
    MyMethod(myInitialString, MyRefMethod(ref myInitialString));
    Console.ReadLine();
}

static void MyMethod(string myVariableString, string otherString)
{
    //Prints out "My Initial String"
    Console.WriteLine($"MyString in myMethod: {myVariableString}");
}

static string MyRefMethod(ref string myRefString)
{
    myRefString = "MyEdittedString";
    Console.WriteLine($"{nameof(myRefString)} = {myRefString}");
    return "OtherString";
}
Run Code Online (Sandbox Code Playgroud)

如果我对此进行调试并进行监视,myInitialString我可以看到它的值"My Editted String"就在它进入MyMethod并保持该值之前.但是这个myVariableString论点的价值是"My Initial String".这似乎意味着在第二个参数之前捕获了变量值,这是一个已经评估过的方法.

如果我在打电话MyRefMethod之前单独拨打电话MyMethod,它就像我期望的那样工作,打印出来"My Editted String".如果我交换参数使得调用MyRefMethod代表传递给的第一个参数MyMethod,它也会打印出来"My …

.net c#

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

为什么在字典<>中添加新值会覆盖其中的先前值

我一直在使用C#中的Dictionary进行此问题.每当我添加一个条目时,字典中的所有条目都填充相同的值.这是代码:

using System;
using System.Collections.Generic;

namespace TESTING
{
    class Program
    {
        /*--------------------Variable Declaration------------------------*/
        public static int NumberOfPlayers = 5;
        public static Dictionary<int, bool> PlayerAI = new Dictionary<int, bool>();
        public static Dictionary<int, Dictionary<string, int>> Players = new Dictionary<int, Dictionary<string, int>>();

        /*----------------------------------------------------------------*/
        public static void Main(string[] args)
        {
            Dictionary<string, int> TMP = new Dictionary<string, int>();
            for (int i = 0; i < NumberOfPlayers; i++)
            {
                Console.WriteLine(i);
                TMP.Clear();
                TMP.Add("Player Target", 0 + (i * 3));
                TMP.Add("Player Cash", 0 + (i * 3));
                TMP.Add("Player …
Run Code Online (Sandbox Code Playgroud)

c# dictionary

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

我想在运行时创建一个对象数组

我正在创建一个版本的河内塔拼图。它在 Unity 层次结构中的表示方式是

-游戏对象
-----PegA
---------Arm
---------Base

抱歉,我不知道如何表示层次结构。

场景中有 3 个“钉子”和 7 个“环”作为对象。钉和环在层次结构中处于同一级别。

很明显,我可以“SerializeField”环类,只需在检查器中单击每个 Peg 并将其拖到它们上,但我想要做的只是在运行时将它们添加到代码中。这是我尝试过的。

这是我的戒指课程的一部分

public class ring : MonoBehaviour
{
    public bool locked, resting;
    private float startX, startY, deltaX, deltaY;
    private Vector3 mousePos, beforeDrag;

    private List<GameObject> pegs;

    // Start is called before the first frame update
    void Start()
    {
        startX = transform.position.x;
        startY = transform.position.y;
        locked = false;
        pegs.Add(GameObject.Find("PegA"));
        pegs.Add(GameObject.Find("PegB"));
        pegs.Add(GameObject.Find("PegC"));
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是“对象引用未设置为对象的实例”

有人可以解释一下吗?

c# unity-game-engine

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