Tag: 数组

为什么数组类没有costructor(为什么它不是通过例如new int ()创建的)

我理解C#中的数组是如何工作的,并且它们的创建语法只是磁带和方括号(例如new int[] ),可选地后跟特殊的括号,其中包含要填充新数组的项目。 但是,该数组在内部是一个(好的,可能不是这样)普通类。 而且它确实有一个隐式构造函数(不一定)应该在创建一个应该被语法反映的数组时调用 – >创建应该是new int[]()而不是它是什么… 但它显然不是那样的,我的问题是为什么……是什么导致他们这样做(不直观)……

如何在C#中将Json转换为Object

我想在C#中将Json转换为Object。 Json这里是: [{ “值”: “e920ce0f-e3f5-4c6f-8e3d-d2fbc51990e4”}]。 如何使用Object。 问题似乎很愚蠢,但并不是那么愚蠢。 我没有简单的Json,我有IEnumerable,我从usint JsonResult得到json像这样: 新的JsonResult(来自于User.Claims中的c,其中(c.Type.Equals(“value”))选择新的{c.Value}); 这个linq代码不适用于JObject。 感谢Tommaso Belluzzo。

如何在堆栈上分配数组以获得性能提升?

一些最佳版本的函数,如popcount和count consecutive zeros使用表查找来获得最终答案。 在C和C ++中,可以在堆栈上分配数组并快速访问它们。 有没有办法在C#中做到这一点? 据我所知, stackalloc只能在函数中使用,因此数组不会持久存在。 我有一个小的查找表,我希望能够尽快访问,因此更愿意在堆栈而不是堆上分配它。

如何在块中使用File.ReadAllBytes

我正在使用此代码 string location1 = textBox2.Text; byte[] bytes = File.ReadAllBytes(location1); string text = (Convert.ToBase64String(bytes)); richTextBox1.Text = text; 但是当我使用一个太大的文件时,我会遇到内存exception。 我想在块中使用File.ReadAllBytes 。 我见过这样的代码 System.IO.FileStream fs = new System.IO.FileStream(textBox2.Text, System.IO.FileMode.Open); byte[] buf = new byte[BUF_SIZE]; int bytesRead; // Read the file one kilobyte at a time. do { bytesRead = fs.Read(buf, 0, BUF_SIZE); // ‘buf’ contains the last 1024 […]

有序独特组合

我有以下课程: internal class Course { public int CourseCode { get; set; } public string DeptCode { get; set; } public string Name { get; set; } } 以下代码是我的二维数组: Course[][] courses = new Course[3][]; courses[0] = new Course[] { new Course() { CourseCode = 100, DeptCode = “EGR”, Name = “EGR A” }, new Course() { […]

如何在模型的侧面填充数组以进行回发

我有一个包含Milestone列表的模型,我希望在网页中使用给定的一组文本框填充(在模型内)列表。 public class Project { public string ProjectNumber { get; set; } public IList Parameters; public IList MilestoneList = new List(); } 在我的Html.BeginForm(“Edit”, “Home”, FormMethod.Post, new { Project = Model }))我有以下TextBox。 @for (int i = 0; i < Model.MilestoneList.Count; i++) { @Html.TextBoxFor(model=>model.MilestoneList[i].Value) } 我的控制器在milestonelist下面的问题在模型Project中始终为null [HttpPost] public ActionResult Edit(Project project) { helper.CreateProject(project, System.Web.HttpContext.Current.User.Identity.Name); return View(); } […]

StartCoroutine被调用了很多次(C#Unity)

我正在Unity中创建一个弹出菜单选项。 现在我的问题是我在无效更新中做的协程被调用了很多次。 我的意思是在我的Unity控制台上,Debug.Logs正在递增。 它应该是正确的,因为它已经是协程。 有些人可以帮助我理解更多的协程并帮助我解决我的小问题。 这是我的代码: [SerializeField] GameObject Option; [SerializeField] Button btn,btn2; [SerializeField] GameObject open, close; [SerializeField] GameObject[] opt; bool startFinding = false; void Start() { Option.SetActive(false); Button popUp = btn.GetComponent(); Button popUp2 = btn2.GetComponent(); popUp.onClick.AddListener(PopUpOption); popUp2.onClick.AddListener(ClosePopUp); } void Update() { if (startFinding) { StartCoroutine(GameOptions()); } } IEnumerator GameOptions() { //Get All the tags opt […]

Visual C#对象数组更改数组中的所有元素而不是一个元素

我是C#的新手所以我正在制作一个蛇游戏来学习。 我创建了一个蛇的对象,其中包含一个名为pieces的对象数组。 所以这些碎片构成了蛇。 我试图将第一个棋子的x值改为1,以便将第一个棋子的一个单位向右移动。 问题是,当我将第一个棋子的x值改为1时,第二个棋子的x值也会改变1.所以myPiece [1] .x = 6和myPiece [2] .x = 6即使我只增加了第一个一个人的价值。 我怎样才能做到这一点,这样我就可以改变每件作品的价值而不改变作品价值的其余部分? class theSnake { private int size; private thePiece[] myPieces=new thePiece[50]; private int xDim; private int yDim; public theSnake(int pxDim,int pyDim) { size = 1; xDim = pxDim; yDim = pyDim; for (int i = 1; i = 2; i–) { myPieces[i] […]

将数组传递给存储过程

我必须将数组和字符串传递给存储过程并返回数据表 C#方: public DataTable fetchRequested(string [] empID, string [] account, string [] refNo, string orgID, string Id, DateTime valueDate) { string connetionString = null; OracleConnection con; OracleDataAdapter objAdapter = null; OracleCommand objComm = new OracleCommand(); connetionString = @”Data Source= Payment_devlope; User ID=ORGPAYMENT;Password=OrgPayment”; con = new OracleConnection (connetionString); try { con.Open(); objComm.Connection = con; objComm.CommandType = […]

将Array转换为主要方法以表示相同的结果,不使用仅LINQ int。

我的程序记录了四个房间在瓶子里收集的瓶子数量。 当用户输入退出时,显示每个房间收集的瓶子数量,并打印出具有最多瓶子的房间。 我用一个数组来跟踪房间号码。 如何更改方法而不是使用数组,我想启动room1,room2,room3,room4。 如果我不使用数组,我是否可以使用我的循环数组调用来写行? 我的意思是。 int room = int.Parse(quit); Console.Write(“Bottles collected in room {0}: “, room); // This line adds the count of bottles and records it so you can continuously count the bottles collected. rooms[room – 1] += int.Parse(Console.ReadLine()); 而这一行: }//Writes the bottles collected by the different rooms Console.WriteLine(“Bottles collected in room {0} […]