const and static readonly - Difference.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kavitha

    #1

    const and static readonly - Difference.

    Hi Pals,
    I am confused between const and static readonly in C#. I want to know the
    difference between then. Please give me clear explanation.
    thanks in advance.
    --
    Kavitha
  • John Paul. A

    #2
    RE: const and static readonly - Difference.

    Dear Kavitha,
    The difference is that the value of const is set to a compile time constant,
    whereas the static readonly is set at runtime and can be modified by the
    containing class (that too only in the variable declaration and in the static
    constructor).

    Note: Use static readonly when the value is not known at compile time.

    Rgds,
    John Paul. A
    MCAD

    "Kavitha" wrote:
    [color=blue]
    > Hi Pals,
    > I am confused between const and static readonly in C#. I want to know the
    > difference between then. Please give me clear explanation.
    > thanks in advance.
    > --
    > Kavitha[/color]

    Comment

    • Cor Ligthert

      #3
      Re: const and static readonly - Difference.

      Kavitha,

      Did you know that there is a special group for language questions.

      microsoft.publi c.dotnet.langua ges.csharp

      Not that you are not welcome here, however that is more for questions as
      this.

      (The Static keyword has a complete different meaning in VBNet by instance)

      Cor


      Comment

      • Kavitha

        #4
        RE: const and static readonly - Difference.

        Can you give me an example please.

        "John Paul. A" wrote:
        [color=blue]
        > Dear Kavitha,
        > The difference is that the value of const is set to a compile time constant,
        > whereas the static readonly is set at runtime and can be modified by the
        > containing class (that too only in the variable declaration and in the static
        > constructor).
        >
        > Note: Use static readonly when the value is not known at compile time.
        >
        > Rgds,
        > John Paul. A
        > MCAD
        >
        > "Kavitha" wrote:
        >[color=green]
        > > Hi Pals,
        > > I am confused between const and static readonly in C#. I want to know the
        > > difference between then. Please give me clear explanation.
        > > thanks in advance.
        > > --
        > > Kavitha[/color][/color]

        Comment

        • John Paul. A

          #5
          RE: const and static readonly - Difference.

          Dear Kavitha,
          Note: Please post the language specific queries to this newsgroup
          microsoft.publi c.dotnet.langua ges.csharp

          Here's the example: The code is self explanatory.

          class TestClass
          {
          public string MachineName;
          }


          class TestMachine
          {
          public static readonly TestClass objTestClass = new TestClass();

          static void Main(string[] args)
          {
          objTestClass.Ma chineName = "PLC 134";

          objTestClass = new TestClass(); // It will give an error
          message
          }
          }

          Rgds,
          John Paul. A
          MCAD

          "Kavitha" wrote:
          [color=blue]
          > Can you give me an example please.
          >
          > "John Paul. A" wrote:
          >[color=green]
          > > Dear Kavitha,
          > > The difference is that the value of const is set to a compile time constant,
          > > whereas the static readonly is set at runtime and can be modified by the
          > > containing class (that too only in the variable declaration and in the static
          > > constructor).
          > >
          > > Note: Use static readonly when the value is not known at compile time.
          > >
          > > Rgds,
          > > John Paul. A
          > > MCAD
          > >
          > > "Kavitha" wrote:
          > >[color=darkred]
          > > > Hi Pals,
          > > > I am confused between const and static readonly in C#. I want to know the
          > > > difference between then. Please give me clear explanation.
          > > > thanks in advance.
          > > > --
          > > > Kavitha[/color][/color][/color]

          Comment

          • Kavitha

            #6
            RE: const and static readonly - Difference.

            Yes I Do. thanks for your code.

            "John Paul. A" wrote:
            [color=blue]
            > Dear Kavitha,
            > Note: Please post the language specific queries to this newsgroup
            > microsoft.publi c.dotnet.langua ges.csharp
            >
            > Here's the example: The code is self explanatory.
            >
            > class TestClass
            > {
            > public string MachineName;
            > }
            >
            >
            > class TestMachine
            > {
            > public static readonly TestClass objTestClass = new TestClass();
            >
            > static void Main(string[] args)
            > {
            > objTestClass.Ma chineName = "PLC 134";
            >
            > objTestClass = new TestClass(); // It will give an error
            > message
            > }
            > }
            >
            > Rgds,
            > John Paul. A
            > MCAD
            >
            > "Kavitha" wrote:
            >[color=green]
            > > Can you give me an example please.
            > >
            > > "John Paul. A" wrote:
            > >[color=darkred]
            > > > Dear Kavitha,
            > > > The difference is that the value of const is set to a compile time constant,
            > > > whereas the static readonly is set at runtime and can be modified by the
            > > > containing class (that too only in the variable declaration and in the static
            > > > constructor).
            > > >
            > > > Note: Use static readonly when the value is not known at compile time.
            > > >
            > > > Rgds,
            > > > John Paul. A
            > > > MCAD
            > > >
            > > > "Kavitha" wrote:
            > > >
            > > > > Hi Pals,
            > > > > I am confused between const and static readonly in C#. I want to know the
            > > > > difference between then. Please give me clear explanation.
            > > > > thanks in advance.
            > > > > --
            > > > > Kavitha[/color][/color][/color]

            Comment

            Working...