473,765 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do we have extention properties in C# 3.0?

Hi,

C# 3.0 extension methods become useful for us. Do we have the similar
concept for extension properties?

Thank you,
Max

Jul 23 '08
26 2856
An extension property therefore should be static, but ... what should
that 'set' do? set a property on arg0?
Yes; as with extension methods the code would explicitely name the arg0
parameter instead of the (explicit or implicit) this in regular instance
members.
Then why not set the property
directly instead of calling this extension property?
Because it might not map to a single property, and we simply want to
encapsulate that logic into a picec of code that is shareable, for
example, over all types (including externals) that implement a
particular interface; to be honest I image "read" extensions properties
would be more common that "write" extension properties, but I can't
think of a good reason to preclude them.

Marc
Jul 24 '08 #11
On Thu, 24 Jul 2008 03:19:24 -0700, Marc Gravell <ma**********@g mail.com>
wrote:
>An extension property therefore should be static, but ... what should
that 'set' do? set a property on arg0?

Yes; as with extension methods the code would explicitely name the arg0
parameter instead of the (explicit or implicit) this in regular instance
members.
Right. And of course, my example seems to imply that you would be able to
code implicit extension properties, which I'm not actually proposing the
language should support (it could, but the implementation would be so ugly
I don't think it should :) ). I just didn't figure I needed to spell out
the get/set implementations for the purpose of demonstrating a possible
syntax.
>Then why not set the property directly instead of calling this
extension property?

Because it might not map to a single property, and we simply want to
encapsulate that logic into a picec of code that is shareable, for
example, over all types (including externals) that implement a
particular interface; to be honest I image "read" extensions properties
would be more common that "write" extension properties, but I can't
think of a good reason to preclude them.
Me either. I can imagine certain cases, for example, where it would be
more convenient to implement a single property that somehow encapsulates
multiple properties on the extended class. Setting that one property
would have some specific effect on the multiple properties.

As with many things, it's hard to know exactly how something might be used
until you make it available and see how people use it. :)

Pete
Jul 24 '08 #12
Peter Duniho <Np*********@nn owslpianmk.comw rote:
Me either. I can imagine certain cases, for example, where it would be
more convenient to implement a single property that somehow encapsulates
multiple properties on the extended class. Setting that one property
would have some specific effect on the multiple properties.

As with many things, it's hard to know exactly how something might be used
until you make it available and see how people use it. :)
Here's a concrete example. I already have a Range<Tclass. It would be
quite nice to give a (readonly - particularly as Range is immutable)
extension property to Range<DateTime> , which retrieved the duration as
a TimeSpan.

Indeed that may end up being one of the primary kinds of use-case:
giving more meaning to one particular closed generic type, but without
needing to add any of the complexities of inheritance.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 24 '08 #13
My biggest complaint with the extension methods is that they don't
support the 'where' clause. You have to specify the exact type. I'd
rather see support for that before adding support for properties. I'd
also like to see the 'where' clause enhanced to support 'primitive',
meaning types that support these operators: +,-,/,*,%, etc. or maybe
some way to specify operators in the 'where' clause.
Jul 25 '08 #14
On Thu, 24 Jul 2008 17:10:38 -0700, not_a_commie <no********@gma il.com>
wrote:
My biggest complaint with the extension methods is that they don't
support the 'where' clause. You have to specify the exact type. I'd
rather see support for that before adding support for properties.
Do you mean something like this:

using System;

namespace TestGenericExte nsionMethod
{
static class Extensions
{
public static void TestMethod<T>(t his T t) where T : TestBase
{
Console.WriteLi ne("{0}.TestMet hod", t.GetType().Nam e);
}
}

class TestBase
{
}

class TestDerived : TestBase
{
}

class Program
{
static void Main(string[] args)
{
TestDerived td = new TestDerived();
Object obj = new Object();

td.TestMethod() ; // works fine
// obj.TestMethod( ); // CS0311 error
Console.ReadLin e();
}
}
}

Your wish is granted! :)
I'd
also like to see the 'where' clause enhanced to support 'primitive',
meaning types that support these operators: +,-,/,*,%, etc. or maybe
some way to specify operators in the 'where' clause.
I think lots of people would like to see useful ways to use generics with
operators like that, whether via a constraint or some other mechanism.
Marc Gravell has posted a useful workaround to the current situation. You
should be able to use Google Groups to look back at the archives and find
his articles on the topic.

Maybe that will also appear in a future C#. It's not a trivial problem to
solve (extension properties would be much easier :) ).

Pete
Jul 25 '08 #15
I'd also like to see the 'where' clause enhanced to support 'primitive',
>meaning types that support these operators: +,-,/,*,%, etc. or maybe
some way to specify operators in the 'where' clause.
As Pete mentions, I've looked at this in the past; a simple answer is
provided here:
http://www.pobox.com/~skeet/csharp/m...operators.html
available as a download from:
http://www.pobox.com/~skeet/csharp/miscutil/

Marc
Jul 25 '08 #16
Your simple case of "<T>(this T t) where T: blah" may work, but the
slightly more complex case of "<T>(this IList<Tlist) where T: blah"
doesn't work properly. The methods show up on all objects.
Jul 25 '08 #17
On Jul 25, 3:39*pm, not_a_commie <notacom...@gma il.comwrote:
Your simple case of "<T>(this T t) where T: blah" may work, but the
slightly more complex case of "<T>(this IList<Tlist) where T: blah"
doesn't work properly. The methods show up on all objects.
That's not happening for me. Could you give a short but complete
example? Here's one from me:

using System;
using System.Collecti ons.Generic;
using System.IO;

public static class Extensions
{
public static void Something<T(thi s IList<Tx)
where T : Stream
{
}
}

public class Test
{
static void Main()
{
List<MemoryStre amy = new List<MemoryStre am>();
y.Something();

List<objectz = new List<object>();
z.Something();
}
}

The z.Something() line fails to compile, with this error:

<quote>
Error 1 The type 'object' cannot be used as type parameter 'T' in the
generic type or method
Extensions.Some thing<T>(System .Collections.Ge neric.IList<T>) '. There
is no implicit reference conversion from 'object' to
System.IO.Strea m'. C:\Documents and Settings\jonske et\My Documents
\Visual Studio 2008\Projects\T est\Test\Progra m.cs 21 9 Test
</quote>

Jon
Jul 25 '08 #18
Here's my test case. It gives an 'ambiguous call' error when it is
clearly not ambiguous.

namespace TestExtensions
{
public interface IOne
{
int Int { get; set; }
}

public class One: IOne
{
public int Int { get; set; }
}

public interface ITwo
{
int Int { get; set; }
}

public class Two : IOne
{
public int Int { get; set; }
}

public static class ExtensionsOne
{
public static void AddFive<T>(this T obj) where T : One
{
obj.Int += 5;
}
}

public static class ExtensionsTwo
{
public static void AddFive<T>(this T obj) where T : Two
{
obj.Int += 5;
}
}

class Program
{
public static void Main(string[] args)
{
var one = new One();
one.AddFive();
}
}
}
Jul 25 '08 #19
I should add that changing class Two to inherit from ITwo makes no
difference.
Jul 25 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2490
by: WindAndWaves | last post by:
Hi Gurus I am just putting my first PHP site together... One thing that I think it strange is that I do not seem to be able to view the source. It is almost like the page does not load properly... If you like then I can email you the source code, but I thought that this may be a problem that occurs frequently...
1
2601
by: Carl Ogawa | last post by:
How do I make .cgi extention work? I installed ActivePerl 5.8. My CGI scripts work fine with .PL extention but not .CGI extention although I associated CGI extention as exactly same as PL extention in WinXP Prof . Where else should I change to make it work?
3
1785
by: Tuvas | last post by:
I am currently writing an extention module that needs to recieve a list of characters that might vary in size from 0 to 8. This is written as a list of characters rather than a string because it's easier to manipulate. However, when I pass this list of characters into the extention module, it keeps giving errors. Is there a way to do one of the following? A. Change a list of chars to a single string or B. Read a list of chars in an...
8
1467
by: Tuvas | last post by:
I am in the process of writing an extention module, and am coming up with lots of problems. Perhaps someone could be of use. I keep getting data that isn't what I'm sending the program. Let me give the line of C code and Python Code and output to illistrate the problem. write_can(can_han,0x0140,'abcd') if (PyArg_ParseTuple(args("iiz#",&can_han,&com,&data,&len) return NULL;
6
4997
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
2
1478
by: Howard | last post by:
how do i change the file extention of asp.net file? ie. from .aspx to .mspx
2
2227
by: ghighi | last post by:
Hello, Il a downloading a file from a download.aspx page with that code: ----------------------------------------------------------------------------------------- string fileName = Request.Params; string fileExtension=fileName.Substring(fileName.Length-3); string filePath = Path.Combine( "apath",fileName); FileStream fs = new FileStream(filePath, FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
0
1024
by: anandansv | last post by:
When i try to install .Net2003 in Win2000 Professional is gives an warning message "Frontpage 2002 server Extention " is not configure. How to configure that/
1
1653
by: godfather96 | last post by:
I am using a web control called eXml which is an extention to ms xml web control which supports xslt 2.0 when trying to group elements i receive the following error: group-by is not a valid extention element is there a namespace or something a need to add. my source is below
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.