Create List with values at compile time

List<int> myValues = new List<int>(new int[] { 1, 2, 3 } ); This will create an intermediate array however so there may be a more efficient way of doing the same thing. EDIT: John Feminella suggested creating a factory method to accept a list of parameters and return a List which you could implement as … Read more

C#: multiline text in DataGridView control

You should set DefaultCellStyle.WrapMode property of column to DataGridViewTriState.True. After that text in cells will be displayed correctly. Example (DataGridView with one column): dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True; dataGridView1.Rows.Add(“test” + Environment.NewLine + “test”); (Environment.NewLine = \r\n in Windows)

Test events with nunit

Checking if events were fired can be done by subscribing to that event and setting a boolean value: var wasCalled = false; foo.NyEvent += (o,e) => wasCalled = true; … Assert.IsTrue(wasCalled); Due to request – without lambdas: var wasCalled = false; foo.NyEvent += delegate(o,e){ wasCalled = true;} … Assert.IsTrue(wasCalled);

static readonly field initializer vs static constructor initialization

There is one subtle difference between these two, which can be seen in the IL code – putting an explicit static constructor tells the C# compiler not to mark the type as beforefieldinit. The beforefieldinit affects when the type initializer is run and knowing about this is useful when writing lazy singletons in C#, for … Read more

How to use LogonUser properly to impersonate domain user from workgroup client

Very few posts suggest using LOGON_TYPE_NEW_CREDENTIALS instead of LOGON_TYPE_NETWORK or LOGON_TYPE_INTERACTIVE. I had an impersonation issue with one machine connected to a domain and one not, and this fixed it. The last code snippet in this post suggests that impersonating across a forest does work, but it doesn’t specifically say anything about trust being set … Read more