0

This was an interview question I was asked. I wasn't sure about the answer.

In C# Both const and static member varables can:

A) Be set in a static constructor, static method, or instance method of a class.
B) Change after a class has been initialized the first time.
C) Only be set in an instance constructor.
D) Be accessed without an instance of a class.
E) Be set by a set accessor of a public property.

I chose "A" even though I was unsure what they meant. I didn't know whether to pick A or E. Since I was running out of time I picked A. I probably should have picked E.

A) My problem with this one is that it says "instance method."
B) They don't change - False
C) Is the constructor of a static method or class called an "Instance Constructor?" I know you can have a static constructor.
D) How can you access a constant without an instance? - FALSE
E) Not sure. I guess this could be true.

Can someone explain? thanks!

1
  • 2
    Well, clearly D). Because all the other answers will either prove not true for either one (or both). Commented Aug 19, 2015 at 1:11

4 Answers 4

4

In C# Both const and static member varables can:

A) Be set in a static constructor, static method, or instance method of a class.
B) Change after a class has been initialized the first time.
C) Only be set in an instance constructor.
D) Be accessed without an instance of a class.
E) Be set by a set accessor of a public property.

A) You can't ever set a constant, you can only define it. You can define a constant anywhere you can define a variable, but the very word set means this choice is wrong.

B) You definitely can't change a constant, so it's not B.

C) Incorrect, as above (A)

D) Fine, you can write MyClass.MyConst and MyClass.MyStatic

E) Incorrect, you can't change a constant.

So the answer is D.

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

Comments

2

The answer was D. Any constant or static member that is public, protected or internal can be accessed outside the class without an instance.

Think of a const as a value type static readonly variable. also, marking them as const lets the compile use optimization at compilation.

3 Comments

actually the compiler optimizations you are talking about is time of evaluation... const is evaluated at compile time while a static is evaluated at runtime.
@Steve The optimizations he's talking about, regarding const, happen at compile time, not time of evaluation. const is essentially a pre-processing directive which replaces all references to the constant with the value of the constant. If you write: const int myConst = 1; var t = myConst; var o = myConst, it will be compiled to int t = 1;int o = 1;
Exactly @Rob, I was not making the distinction between const and static readonly, just pointing out that const is preferred int the case of a value type "constant value"
1

The answer is D.

A static member or property and a const can be accessed without an instance.

i.e

var x = Foo.StaticProperty; 

Think of const as a static value that can not be changed at runtime.

E is incorrect because you cannot set a const at all.

1 Comment

Thanks for taking the time to answer my question! I appreciate the feedback. So you are saying that you don't need an instance to use a property or a static property? Thanks! I guess I have some homework to do.
1

Quick breakdown of the answers:

A) False, since consts don't get set in the static constructor.

B) False, you can't change the value of a const after declaring it

C) False, again, can't set consts after declaring it. You can set static variables in an instance method (and ctors) but can't change a const once you define it.

D) True - you don't need an instance of the class in order to access either.

E) False, again, consts can't be changed...

1 Comment

Thanks for taking the time to answer my question! I appreciate the feedback

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.