好吧,我正在尝试做的基本思路是将字节数组转换为short或int等.
一个简单的例子可能是:
unsafe
{
fixed (byte* byteArray = new byte[5] { 255, 255, 255, 126, 34 })
{
short shortSingle = *(short*)byteArray;
MessageBox.Show((shortSingle).ToString()); // works fine output is -1
}
}
Run Code Online (Sandbox Code Playgroud)
好的,所以我真正要做的是,对Stream类进行扩展; 扩展的读写方法.我需要以下代码的帮助:
unsafe public static T Read<T>(this Stream stream)
{
int bytesToRead = sizeof(T); // ERROR: Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')
byte[] buffer = new byte[bytesToRead];
if (bytesToRead != stream.Read(buffer, 0, bytesToRead))
{
throw new Exception(); …Run Code Online (Sandbox Code Playgroud) List<bool> test = new List<bool>();
test.Sort(new Func<bool, bool, int>((b1, b2) => 1));
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
错误2参数1:无法从'System.Func'转换为'System.Collections.Generic.IComparer'
错误1'System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)'的最佳重载方法匹配有一些无效的参数
当我有
private int func(bool b1, bool b2)
{
return 1;
}
private void something()
{
List<bool> test = new List<bool>();
test.Sort(func);
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.他们不是一回事吗?
private SortedList<ToolStripMenuItem, Form> forms = new SortedList<ToolStripMenuItem, Form>();
private void MainForm_Load(object sender, EventArgs e)
{
formsAdd(menuCommandPrompt, new CommandPrompt());
formsAdd(menuLogScreen, new LogScreen()); //Error
}
private void formsAdd(ToolStripMenuItem item, Form form)
{
forms.Add(item, form); //Failed to compare two elements in the array.
form.Tag = this;
form.Owner = this;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么它会抛出错误.第二行表单加载事件发生错误.
formsAdd方法只是将form和toolstip元素添加到数组(表单)并将表单的标记和所有者设置为此.在第二次调用函数时,它会抛出一个错误.
CommandPrompt, LogScreen /* are */ Form //s
menuCommandPrompt, menuLogScreen /* are */ ToolStripMenuItem //s
Run Code Online (Sandbox Code Playgroud)

class Monitor
{
private:
int rate;
Monitor(int rate)
{
this.rate = rate;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码.我尝试使用通过构造函数"rate"的本地成员设置私有成员"rate".但是我可以在C#中使用它.
错误是:
表达式必须具有类类型
class Program
{
static void Main()
{
int i = 0;
whatever x = new whatever(i);
Console.WriteLine(x);
i = 1;
Console.WriteLine(x);
Console.ReadKey();
}
class whatever
{
public whatever(object variable)
{
this.variable = () => variable.ToString();
}
private Func<string> variable;
public string data;
public override string ToString()
{
data = variable();
return data;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
0
Run Code Online (Sandbox Code Playgroud)
我想要做的是更新我的价值.
可能重复:
C#中属性和字段之间的差异
public class Test
{
public bool testData1;
public string testData2;
}
Run Code Online (Sandbox Code Playgroud)
要么
public class Test
{
public bool TestData1 { get; set; }
public string TestData2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
要么
public class Test
{
private bool testData1;
private string testData2;
public bool TestData1 { get { return testData1; } set { testData1 = value; } }
public string TestData2 { get { return testData2; } set { testData2 = value; } }
}
Run Code Online (Sandbox Code Playgroud)
哪种优化代码更好或更不必要?为什么?
这不是最后一个持有很多不必要的数据吗?
=======编辑: …