Is it possible to get value without creating an instance ?
I have this class:
public class MyClass
{
public string Name{ get{ return "David"; } }
public MyClass()
{
}
}
Now I need get the value "David", without creating instance of MyClass.
Real answer: no. It's an instance property, so you can only call it on an instance. You should either create an instance, or make the property static as shown in other answers.
See MSDN for more information about the difference between static and instance members.
Tongue-in-cheek but still correct answer:
Yes, but only via some really horrible code which creates some IL passing in
null
asthis
(which you don't use in your property), using aDynamicMethod
. Sample code:Please don't do this. Ever. It's ghastly. It should be trampled on, cut up into little bits, set on fire, then cut up again. Fun though, isn't it? ;)
This works because it's using
call
instead ofcallvirt
. Normally the C# compiler would use acallvirt
call even if it's not calling a virtual member because that gets null reference checking "for free" (as far as the IL stream is concerned). A non-virtual call like this doesn't check for nullity first, it just invokes the member. If you checkedthis
within the property call, you'd find it's null.EDIT: As noted by Chris Sinclair, you can do it more simply using an open delegate instance:
(But again, please don't!)
Create a static property:
Get it like so:
You requirements do seem strange, but I think you're looking for some kind of metadata. You can use an attribute to achieve this:
Now this code can get names with and without instances:
Create a static class or a static property, and you don't have to explicitly instantiate it.
You can make your property static, as pointed out by many others.
Be aware that this means your instances of MyClass will no longer have their own Name property, since static members belong to the class, not the individual object instances of it.
Edit: In a note, you mentioned that you want to override the Name property in subclasses. At the same time, you want to be able to access it at the class level (access it without creating an instance of your class).
For the static properties, you would simply create a new
Name
property in each class. Since they arestatic
, you're always (almost always, yay reflection) going to access them using a specific class, so you'd be specifying which version ofName
you want to get. If you want to try and hack polymorphism in there and get the name from any given subclass of MyClass, you could do so using reflection, but I wouldn't recommend doing so.Using the example from your comment:
As a side note, since you are declaring a property that has only a getter and it is returning a constant value, I recommend possibly using a const or static readonly variable instead.Usage for both would be the same:
The main benefit (and drawback) of
const
is that all references to it are actually replaced by its value when the code is compiled. That means it will be a little faster, but if you ever change its value, you will need to recompile ALL code that references it.You can make that property static
Usage: