Intrinsic Attributes in c#

Attributes are a mechanism of adding metadata or declarative information to program element such as class, methods, constructor, property etc. Intrinsic attributes are supplied as part of the Common Language Runtime and they are integrated into .NET framework. In this article we will understand few basic Intrinsic attributes that are easy to understand.

Obsolete Attribute
This attribute accepts two parameters of which the first is a message and the second a boolean value. If the second parameter is true, the compiler gives an error if the method is invoked, else a warning message is displayed.
  1. using System;  
  2. namespace Attributes  
  3. {  
  4.     class Program  
  5.     {  
  6.         [Obsolete("This method is no more required"true)]  
  7.         void Display()  
  8.         {  
  9.         }  
  10.         void Display(string s)  
  11.         {  
  12.             Console.WriteLine(s);  
  13.         }  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Program p = new Program();  
  17.             p.Display();  
  18.         }  
  19.     }  
  20. }    
Above error message is display on compilation as boolean value is set to true.
 
 Serialization Attribute
Serialization is the process of converting complex objects into stream of bytes for storage. Storing objects into disk directly is very dangerous hence with serialization attribute and ISerializable interface we can write any complex object directly to a filestream. I have implemented very basic serialization code below:
  1. namespace Attribute  
  2. {  
  3.     using System;  
  4.     using System.IO;  
  5.     using System.Runtime.Serialization;  
  6.     using System.Runtime.Serialization.Formatters.Binary;  
  7.     [Serializable()]  
  8.     public class Student : ISerializable  
  9.     {  
  10.         public string StudentName;  
  11.         public Student()  
  12.         {  
  13.             StudentName = null;  
  14.         }  
  15.         public void GetObjectData(SerializationInfo info, StreamingContext ctxt)  
  16.         {  
  17.      
  18.             info.AddValue("Name", StudentName);  
  19.         }  
  20.         static void Main(string[] args)  
  21.         {  
  22.             Student student = new Student();  
  23.             student.StudentName = "Eliza";  
  24.             //Serialization using file stream  
  25.             Stream stream = File.Open("StudentDetail.txt", FileMode.Create);  
  26.             BinaryFormatter bformatter = new BinaryFormatter();  
  27.             bformatter.Serialize(stream, student);  
  28.             stream.Close();  
  29.         }  
  30.     }  
  31. }  
Here we are serializing student class properties using BinaryFormatter. Once we compile the code student class property "StudentName" gets serialized and stored inside "StudentDetails.txt" which is atleast not human readable.
StudentDetails.txt file content:
 
In Order to Deserialize we need to do two thing, first need to create an Deserialize constructor and second need to call BinaryFormatter again.
Deserialize Constructor:
  1. public Student(SerializationInfo info, StreamingContext ctxt)   
  2.         {  
  3.             StudentName = (String)info.GetValue("Name", typeof(string));  
  4.         }  
 
Here Deserialize constructor reads the value of "Name" key inside file stream.
 //Deserialization code
            stream = File.Open("StudentDetail.txt", FileMode.Open);
            bformatter = new BinaryFormatter();
            student = (Student)bformatter.Deserialize(stream);
            Console.WriteLine("Employee Name: {0}", student.StudentName);
Hence on deserialization we get below console output:
 
Ebook Download
View all
Learn
View all