Click here to Skip to main content
15,888,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

If I have created a Class for instance named Foo. How do I set a default property so for instance:

C#
Foo f = new Foo();
f = "Test";


Is it possible without having to use a constructor or having to...
C#
f.Name = "Test"
If it is is there an Attribute that needs to be set or what should the underlying Propery be called in the Foo Class for this to happen?

Thanks in advance.
Posted

Have a look at object initializers[^].

If you class Foo contains a property called Name, you can do Foo f = new Foo{Name="Test"}
 
Share this answer
 
Comments
Member 8255180 3-Jan-12 12:55pm    
Thank you, but is it possible using a Parameterless Constructor?
theanil 3-Jan-12 12:57pm    
5+
Abhinav S 3-Jan-12 12:58pm    
Thanks.
Abhinav S 3-Jan-12 12:59pm    
I dont understand your question completely. Your constructor will be parameterless.
Member 8255180 3-Jan-12 14:13pm    
My apologise, I didn't read the answer correctly...I meant without having to specify the Properties in the Object Initialiser. I have searched and I did not if there was an Attribute etc that I could set so, for instance f="Test" automatically goes to the Name Property
There is no such feature in C#. Many C# people would not even understand your question because they might be unfamiliar with this concepts know in some other languages. Not a very big loss, I would say…

—SA
 
Share this answer
 
v2
Comments
mostafa_darwiche 3-Jan-12 17:07pm    
What is the language that supports the above situation?
Sergey Alexandrovich Kryukov 4-Jan-12 11:56am    
In Object Pascal and Delphi Pascal you can have default property. In C++ you can create some similar effect by defining assignment operator...
--SA
Member 8255180 3-Jan-12 17:12pm    
Thank you all for your help with this. I guess, as we all do, like to make our code as clean as possible. C# is a great language but as I have seen today it does have it's limitations.

After studying the book "CLR with C#" by Jeffrey Richter I could code in the Class..

public string this[int i]
{
get{return myvalue;}
set{myvalue = value;}
}

Then in the calling method...

foo[0]="Test";

This seems to work, but why do I have to use an Indexer???



Sergey Alexandrovich Kryukov 4-Jan-12 11:59am    
You don't have to. The indexer is syntactic sugar, another one. A default property is also syntactic sugar, but it does not exist in C#. You better use it only when the value of the index means something. An index can be anything: Boolean, string...

In your case all you need is some named property like "Name". What's wrong with it?
I think the topic is closed by now.
--SA
Despite I wouldn't do it, as it breaks some simple rule of OOP, you can use an implicit operator :

public class Foo
{
   public string Name { get; private set; }

   public static implicit operator Foo(string s)
   {
      return new Foo { Name = s };
   }
}


Which would allow you to write :

Foo f = "some string";


But, again, I wouldn't do it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900