Skip to content

18. PropertySpecified, Out, Attributes

February 27, 2011

52. What is the significance of the “PropertySpecified” Pattern used by the XMLSerializer? What problem does it attempt to solve? The XMLSerializer enables how to control how objects are encoded into XML. Serializes and deserializes objects into and from XML documents. In .NET 1.x ithe PropertySpecified pattern tried to solve the problem that numeric values such as int and float could not be null, so it had no other way to indicate that the attribute/element existed in the XML file or not. Since .NET 2.0 one can use Nullable<T> and for example int? instead.

53. Why are out parameters a bad idea in .NET? Are they?
The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. Although variables passed as an out argument need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.
Example of using out keyword 
On one hand they shouldn’t be overused since they can make the code a little bit less easy to understand. In most cases there’s a better way to accomplish the intent of a method that uses an “out” parameter and the method can be refactored to return the information. On the other hand Dictionary<K,V> has a TryGeyValue() method which is a good example of why out parameters can be a nice thing to have.

54. Can attributes be placed on specific parameters to a method? Why is this useful?
Attributes provide a powerful method of associating metadata, or declarative information, with code(assemblies, types, methods, properties and so forth). After an attribute is associated with a program entity, the attributes can be queried at run time by using a technique called reflection. Attributes have the following properties: 1.Attributes add metadata to the program. Metadata is information about the types defined in the program. All .NET assemblies contain a specified set of metadata that describes the types and type members defined in the assembly. You can add custom attributes to specify any additional information that is required. 2.You can apply one or more attributes to entire assemblies, modules or smaller program elements such as classes and properties. 3.Attributes can accept arguments in the same way as methods and properties. 4.Your program can examine its own metadata or the metadata in other programs by using reflection. Attributes can be placed on most any declaration, though a specific attribute might restrict the types of declarations on which it is valid. In C#, you specify an attribute by placing the name of the attribute, enclosed in square brackets([]), above the declaration of the entity to which it applies. In Visual Basic, an attribute is enclosed in angle brackets(<>). It must appear immediately before the element to which it is applied on the same line.
In this example the SerializableAttribute attribute is used to apply a specific characteristic to a class:

SerializableAttribute applied to a class
A method with the attribute DllImportAttribute is declared like this:
More than one attribute can be placed on a declaration:
More than one attribute placed on a declaration

Happy Programming! =)

Source: mainly MSDN and

From → OOP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: