Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 05-05-2006, 07:38 AM   #1 (permalink)
 
Join Date: Feb 2003
Posts: 33
Post C# and .Net Questions

I'am do not have a question but many solution .

Ok some nice hint at first I want to tell everyone.
However this is not realy needed unless you whant realy create tricky but very fast code for special use cases.

Structures are realy nic, they are fast and have no overhead.
A special thing that can be done with structs is:

[StructLayout(LayoutKind.Explicit, Size = 2)]
public struct Memory
{
[FieldOffset(0)]
public ushort register16Bit;
[FieldOffset(0)]
public byte register8BitA;
[FieldOffset(1)]
public byte register8BitB;
}

Just keep in mind windows is Litte Endian so register8BitB is the higher byte of register16Bit and register8BitA is the lower byte.
For multiplatform (only if the other platform is an Big Endian System). Endianess can be aked by using BitConverter.IsLittleEndian or setting one 8Bit register of the Memory structure and check the value of register16Bit.
swtrse is offline   Reply With Quote
Old 05-08-2006, 07:40 AM   #2 (permalink)
 
Join Date: Feb 2003
Posts: 33
Default

Im back with the next tipp of the week ^^.

Instead of using huge constructs of if's like
Code:
if (x == 1)
{
   ...
}
else if(x == 2)
{
   ...
}
else if (x == 3)
{
   ...
}
else
{
   ...
}
use a switch statement like
Code:
switch(x)
{
   case 1:
      ...
      break;
   case 2:
      ...
      break;
   case 3:
      ...
      break;
   default:
      ...
      break;
}
it is much more readable and faster that the ifs.
If you have an if with many or's in the condition like.
Code:
if(x== 1 || x==2 || x == 3)
you can write
Code:
switch(x)
{
   case 1:
   case 2:
   case 3:
      ...
      break;
}
instead.

Beware you can only fall through one case label to an other if ther is no code in the upper case lable.
This will work.
Code:
int y = 0;
switch(x)
{
   case 1: 
   case 2:
      ++y;
      break;
}
This will give you an error.
Code:
int y = 0;
switch(x)
{
   case 1: 
      ++y;
   case 2:
      ++y;
      break;
}
If you know what you are doing you can prevent the error with
Code:
int y = 0;
switch(x)
{
   case 1: 
      ++y;
      goto case 2;
   case 2:
      ++y;
      break;
}
swtrse is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5