constants and why override .tostring() method

What are constants in C#?
Constants in C# are immutable values which are known at compile time and do not change for the life of the program. Constants are declared using the const keyword. Constants must be initialized as they are declared. You cannot assign a value to a constant after it isdeclared. An example is shown below.

using System;
class Circle
{
   public const double PI = 3.14;
   public Circle()
   {
      //Error : You can only assign a value to a constant field at the time of declaration
      //PI = 3.15;
   }
}
class MainClass
{
   public static void Main()
   {
      Console.WriteLine(Circle.PI);
   }
}

Can you declare a class or a struct as constant?
No, User-defined types including classes, structs, and arrays, cannot be const. Only the C# built-in types excluding System.Object may be declared as const. Use the readonly modifier to create a class, struct, or array that is initialized one time at runtime (for example in a constructor) and thereafter cannot be changed.


Does C# support const methods, properties, or events?
No, C# does not support const methods, properties, or events.

Can you change the value of a constant filed after its declaration?
No, you cannot change the value of a constant filed after its declaration. In the example below, the constant field PI is always 3.14, and it cannot be changed even by the class itself. In fact, when the compiler encounters a constant identifier in C# source code (for example, PI), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference.

using System;
class Circle
{
   public const double PI = 3.14;
}

How do you access a constant field declared in a class?
Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them. Expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant to access the constant. In the example below constant field PI can be accessed in the Main method using the class name and not the instance of the class. Trying to access a constant field using a class instance will generate a compile time error.

using System;
class Circle
{
   public const double PI = 3.14;
}
class MainClass
{
   public static void Main()
   {
      Console.WriteLine(Circle.PI);
      Circle C = new Circle();
      // Error : PI cannot be accessed using an instance
      // Console.WriteLine(C.PI);
   }
}

Q:What is diff between const and read only?
A:Read only value can be change at run time however const value can'r ever change.


Why should you override the ToString() method?
All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below.

using System;
public class MainClass
{
  public static void Main()
  {
   int Number = 10;
   Console.WriteLine(Number.ToString());
  }
}

In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method.

If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class.

using System;
public class Customer
{
 public string FirstName;
 public string LastName;
}
public class MainClass
{
 public static void Main()
 {
  Customer C = new Customer();
  C.FirstName = "David";
  C.LastName = "Boon";
  Console.WriteLine(C.ToString());
 }
}



The code sample below shows how to override the ToString() method in a class, that would give the output you want.


using System;
public class Customer
{
  public string FirstName;
  public string LastName;

  public override string ToString()
  {
    return LastName + ", " + FirstName;
  }
}
public class MainClass
{
  public static void Main()
  {
    Customer C = new Customer();
    C.FirstName = "David";
    C.LastName = "Boon";
    Console.WriteLine(C.ToString());
  }
}

Conclusion : If you have a class or a struct, make sure you override the inherited ToString() method.

Comments

erection pills said…
Hi mates, its enormous post about tutoringand fully explained, keep it up all the time.

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender