Friday, January 4, 2013

Friday, September 23, 2011

Remember the old days: The bitwise operations

Take a look at this great article, written by Alessandro Zifiglio, remembering programming basics and something most of the developers forget and unfortunately use too little. In the good old days the bitwise operators were part of our programmer everyday life. Nowadays they are used only by the good developers which still think in optimizations and performance issues.

Bitwise operators in c# OR(|), XOR(^), AND(&), NOT(~)
  
  
  
  

Tuesday, August 17, 2010

Using PowerCfg to Evaluate System Energy Efficiency

Use PowerCfg to Evaluate Energy Efficiency of your laptop. Just type the following command powercfg –ENERGY via command prompt To have more knowledge about this command, just follow the above link. http://www.microsoft.com/whdc/system/pnppwr/powermgmt/powercfg.mspx

Friday, June 4, 2010

Convert string[] to ArrayList And ArrayList to string[]

This is how you can convert a string[] into an ArrayList in c#
  
  
// string[] -> ArrayList
string[] msg = this.message.Split('\n');

ArrayList arrayList = new ArrayList();
arrayList.AddRange(msg);
  
// ArrayList -> string[]
string[] arrayStrings = arrayList.ToArray(typeof(string)) as string[];

Thursday, May 27, 2010

Password Property in a PropertyGrid

If you have a property in a class to keep track of a password, and you want that class to be editable in a PropertyGrid, but you don't want the password to be human readable, then you just need to add the attribute PasswordPropertyTextAttribute to your property and set the in argument to true, like the example below. This way the users will see circles instead of the pressed keys.
  
[PasswordPropertyTextAttribute(true)]
public string Password { get; set; }

Saturday, June 6, 2009

Microsoft Surface

I've been working for a week now, with the new Microsoft hardware, Microsoft Surface.

Microsoft Surface, is a multi touch product from Microsoft which is developed as a software and hardware combination technology that allows a user, or multiple users, to manipulate digital content by the use of natural motions, hand gestures, or physical objects.

The Microsoft Surface was used by MSNBC during its coverage of the 2008 US presidential election.

Microsoft Surface is a surface computing platform that responds to natural hand gestures and real world objects. It has a 360-degree user interface, a 30-inch reflective surface with a XGA DLP projector underneath the surface which projects an image onto its underside, while four cameras in the machine's housing record reflections of infrared light from objects and human fingertips on the surface.

The surface is capable of object recognition, object/finger orientation recognition and tracking, and is multi-touch and is multi-user. Users can interact with the machine by touching or dragging their fingertips and objects such as paintbrushes across the screen, pictures or by placing and moving placed objects.

Surface has been optimized to respond to 52 touches at a time.

Using the specially-designed barcode-style "Surface tags" on objects, Microsoft Surface can offer a variety of features, for example automatically offering additional wine choices tailored to the dinner being eaten based on the type of wine set on the Surface, or in conjunction with a password, offering user authentication.

A commercial Microsoft Surface unit is $12,500 (unit only), however Microsoft said it expects prices to drop enough to make consumer versions feasible in 2010.

It's being a good experience. You can program it with WPF, with different objects, for that you have to install the Microsoft Surface SDK.

I'll try to put here some examples, and some source code.

Sunday, May 17, 2009

Optimize C# Code... 2

Optimize C# Code... 2

Code optimization is an important aspect of writing an efficient C# application. The following tips will help you increase the speed and efficiency of your C# code and applications.

1. Use of the operator ?: and ??

The conditional operator ?: returns one of two values depending on the value of a Boolean expression. The conditional operator is of the form:

condition ? first_expression : second_expression;

If condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated.


if(i == 0) str = "Zero" else str = "Non Zero";


using the conditional operator, it will be as follow:


string str = x == 0 ? "Zero" : "Non Zero";


The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types.
It returns the left-hand operand if it is not null; otherwise it returns the right operand.


if (string.IsNullOrEmpty(str))
newstr = "i don't wanna be null"
else
newstr = str;



using the conditional operator, it will be as follow:


string newstr = str ?? "i don't wanna be null";


2. TryParse and Parse
The big difference between TryParse and Parse, is that the first one will return a boolean value, indicating if the conversion was successfull or not, and the second one will give you an exception if the convertion was not possible.


string AreYouADeveloper = "true"

if (bool.TryParse(AreYouADeveloper, out boolValue))
{
MessageBox.Show("welcome developer guy")
}
else
{
MessageBox.Show("You Are Not a Developer")
}



3. Exceptions cause performance to suffer significantly
Since exceptions cause performance to suffer significantly, you should never use them as a way to control normal program flow, so the best way is to write code that avoids exceptions. If it is possible to detect in code a condition that would cause an exception, do so. Do not catch the exception itself before you handle that condition.

4. .Net System.Diagnostics.DebuggerStepThrough Attribute
Sometimes when you are running your code in debug mode, you don't want to stop in some methods, maybe because you already know that this method will always return what you want, because is not so important for the job you have in hands, or some other reasons... That's where the DebuggerStepThrough comes in. The compiler will add the necessary metadata preventing the debugger to step into the method. Even if you have this attribute defined, you can stop in, if you define a breakpoint there.


public string Name
{
[System.Diagnostics.DebuggerStepThrough]
get { return name; }
}