Varia

Access modifiers

public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private
The type or member can be accessed only by code in the same class or struct.

protected
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

abstract
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Internals visible to
[assembly: InternalsVisibleTo("namespace")]
Will make all members visible to another dll.

Get version

Component

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
Version.MajorRevision: always 0

Application – Assembly information - Assembly version

1st number = Version.Major
2nd number = Version.Minor
3rd number = Version.Build
4th number = Version.Revision / Version.MinorRevision

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() shows all 4 numbers.

Messagequeue

using System.Messaging
System.Messaging.MessageQueue messageQueue;
messageQueue = new System.Messaging.MessageQueue(".\Private$\pelican"); // not local: "FORMATNAME:DIRECT=OS:pentium06\Private$\pelican"
messageQueue.Formatter = new BinaryMessageFormatter();
messageQueue.Send("MessageObject", "Message");

Mind: you can not send a non-transactional message to a transactional messagequeue!

Transactions

Add System.Transactions to the References
using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
{ ... }
ts.Complete();

Execute command

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = "test.txt";
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
txtCommandlineOutput.Text = p.StandardOutput.ReadToEnd();

Get the user

string name = WindowsIdentity.GetCurrent().Name;

Insert/Update/Delete dialog forms - See also Naming conventions

xIUD iud = new xIUD(1);
DialogResult result = iud.ShowDialog();
if (result == DialogResult.OK)
  ...;
iud = null;
public xIUD(Int16 mode)
{
  InitializeComponent();
  if (mode == 1)
    this.Text = "Insert x";
  else if (mode == 2)
    this.Text = "Update x";
  else if (mode == 3)
    this.Text = "Delete x";
  else
  {
    MessageBox.Show("Mode not supported");
    this.Close();
  }
}
private void btnOK_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.OK;
}

Stupid things I always forget

Set mouse pointer

// Winforms:
Cursor.Current = Cursors.WaitCursor;
Cursor.Current = Cursors.Default;
// WPF:
Mouse.OverrideCursor = Cursors.Wait;
Mouse.OverrideCursor = null;

Object initializer

Message m = new Message { Description = "Test message.", Type = 4 };

Ternary operator

string code = a < b ? "a<b" : "a>b";

Check for null

null coalescing operator ??:
long number = contract.ContractGroup.AddToExistingMcr ?? 0;
null conditional operator ? () Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand).:
decimal secondaryPeriod = pv?.DecimalValue ?? 0;

Case

switch (x)
{
case "A": return "0";
case "B": return "1";
default: return "2";
}

Write to…

The output window: System.Diagnostics.Debug.WriteLine("Some text");
The C# interactive window: Console.WriteLine("Some text");

Enter

Environment.NewLine

Microsoft Office references

com Microsoft Office 14.0 Object Library -> Microsoft.Office.Core
com Microsoft Excel 14.0 Object Library -> Microsoft.Office.Core + Microsoft.Office.Interop.Excel

Wait for x seconds

System.Threading.Thread.Sleep(x * 1000);
Remark:
Thread.Sleep will stop the executing thread for given amount of time. However if you want remain responsive (UI doesn't freeze) you might consider timers.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License