Tips and Tricks in a world of Mix

Posts tagged ‘C#’

Yield & readonly

Yield interacts with the foreach-loop. It is a contextual keyword: yield is a keyword only in certain statements. It allows each iteration in a foreach-loop be generated only when needed. In this way it can improve performance.

The nice things about using yield return is that it’s a very quick way of implementing the iterator pattern, so things are evaluated lazly.

Iterator pattern provides a way to traverse (iterate) over a collection of items without detailing the underlying structure of the collection.

 

 

readonly The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

If you use a const in dll A and dll B references that const, the value of that const will be compiled into dll B. If you redeploy dll A with a new value for that const, dll B will still be using the original value.

If you use a readonly in dll A and dll B references that readonly, that readonly will always be looked up at runtime. This means if you redeploy dll A with a new value for that readonly, dll B will use that new value.

http://stackoverflow.com/questions/277010/what-are-the-benefits-to-marking-a-field-as-readonly-in-c/312840#312840

http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

New Vs Override

http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx

The new modifier instructs the compiler to use your implementation instead of the base class implementation. Any code that is not referencing your class but the base class will use the base class implementation.

http://msdn2.microsoft.com/en-us/library/ebca9ah3.aspx

The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.

http://social.msdn.microsoft.com/Forums/en-US/65e02299-300f-4b74-8f0a-679f490605f5/new-vs-override-?forum=Vsexpressvcs

Design Patterns Quick Reference

http://www.mcdonaldland.info/2007/11/28/40/

http://www.cafepress.com/codergear/5033878

Convert a single DataRow into an object of type T

   1: /// <summary>

   2:        /// Convert a single DataRow into an object of type T.

   3:        /// </summary>

   4:        /// <explanation>

   5:        /// where T : class, new() a constraint on the generic parameter. it must be a class (or reference type), 

   6:        /// and must have an parameterless default constructor.

   7:        /// </explanation>

   8:        public static T ConvertDataRowToEntity<T>(DataRow row) where T : class, new()

   9:        {

  10:            Type objType = typeof(T);

  11:            T obj = Activator.CreateInstance<T>(); //hence the new() contsraint of T object 

  12:            Debug.WriteLine(objType.Name + " = new " + objType.Name + "();");

  13:            foreach (DataColumn column in row.Table.Columns)

  14:            {

  15:                ///by column in the table we get the property in objType ignoring the unneeded ones

  16:                PropertyInfo property =  objType.GetProperty(column.ColumnName,   

  17:                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

  18:                if (property == null || !property.CanWrite)

  19:                {

  20:                    Debug.WriteLine("//Property " + column.ColumnName + " not in object");

  21:                    continue; //or throw

  22:                }

  23:                //}

  24:  

  25:                ///in case the type of the property is different from data object type - 

  26:                ///convert it to the right type

  27:                object value = row[column.ColumnName];

  28:                if (value == DBNull.Value) value = null;

  29:                else

  30:                {

  31:                    Type propertyType = property.PropertyType;

  32:                    Type dataObjectType = row[column.ColumnName].GetType();

  33:                    if (propertyType.ToString() != dataObjectType.ToString())

  34:                    {

  35:                        value = Convert.ChangeType(value, propertyType);

  36:                    }

  37:                }

  38:                property.SetValue(obj, value, null);

  39:                Debug.WriteLine("obj." + property.Name + " = row[\"" + column.ColumnName + "\"];");

  40:            }

  41:            return obj;

  42:        }

Calling a WCF service from Javascript – very good

source : http://dotnetbyexample.blogspot.com/2008/02/calling-wcf-service-from-javascript.html

In a previous post I pointed out how to call an ASP.NET ASMX web service from javascript in the browser. Although this still works fine, the world has moved on to .NET 3.5 and WCF services have now replaced ASMX services. Luckily, these are also callable from javascript although the procedure is now a little bit different. In this sample I presume you can use Visual Studio 2008 and know a little about WCF.

(more…)

Tag Cloud

%d bloggers like this: