1

Here is my c# class code

 public class ConstReadOnly
{
    public const int cV = 10;
    public readonly int rV = 40;
}

Now when i try to create an instance of this class i'm not getting the const variable cV.What may be the reason.

0

2 Answers 2

5

const are implicitly static, you can access them through class name like:

ConstReadOnly.cV

You may see this post from Jon Skeet - Why can't I use static and const together?

Sign up to request clarification or add additional context in comments.

Comments

0

Because constants are implictly static.

If you want to use them, use the class name as the qualifier.

 int fromConstInt = ConstReadOnly.cV;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.