1

Topic: How to declare in a class property on ?

Whether
it is possible to declare somehow in a class on C# property by default?

For example, there is class MyClass with List inside. It is necessary, that it was possible to write myClass. Count, instead of myClass.list. Count

2

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

Whether I> It is possible to declare somehow in a class on C# property by default?
I> For example, there is class MyClass with List inside. It is necessary, that it was possible to write myClass. Count, instead of myClass.list. Count




public class MyClass {List list = new List (); public int Count {get {return list. Count}}} 

3

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

Whether I> It is possible to declare somehow in a class on C# property by default?
I> For example, there is class MyClass with List inside. It is necessary, that it was possible to write myClass. Count, instead of myClass.list. Count


Is
DefaultPropertyAttribute but it does at all that is necessary.


Default Properties remained in , but
with changes, in particular, "Visual Basic 2008 does not support parameterless default properties, and consequently does not allow this programming shortcut.".

4

Re: How to declare in a class property on ?

Hello, the Anonymous author, you wrote:



public class MyClass {List list = new List (); public int Count {get {return list. Count}}} 



Then it is necessary to declare everything that is able List so it is not interesting, Count I only for the task explanation resulted...

5

Re: How to declare in a class property on ?

Hello, _FRED _, you wrote:




This language is not supported or no code example is available. 



Shortly and clearly... Thanks for the help!

6

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

Whether I> It is possible to declare somehow in a class on C# property by default?
I> For example, there is class MyClass with List inside. It is necessary, that it was possible to write myClass. Count, instead of myClass.list. Count


http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx

7

Re: How to declare in a class property on ?

Hello, Rystsov Denis





using System; 
using System. Collections. Generic; 

namespace ConsoleApplication1 
{ 
  class Program 
  { 
    class MyList 
    { 
      private List <int> list = new List <int> (); 
      public static implicit operator List <int> (MyList l) {return l.list;} 
      public MyList () {list. Add (1); list. Add (2); list. Add (3);} 
    }

    static void Main (string [] args) 
    { 
      MyList myList = new MyList (); 
      for (int i=0; i <((List <int>) myList).Count; i ++) 
        Console. WriteLine (((List <int>) myList) [i].ToString ()); 
      Console. ReadLine (); 
    }

  }
}





So it turns out, but not absolutely that, explicit conversion is required:
((List <int>) myList
).Count; without it does not want. And it would be desirable simply
myList. Count, myList. Add, myList []...

8

Re: How to declare in a class property on ?

And if so?

I>

I> static void Main (string [] args) 
I> { 
I>       
[b]List <int> myList = (List <int>) new MyList ();[/b] 
I> for (int i=0; i <myList. Count; i ++) 
I> Console. WriteLine (myList [i].ToString ()); 
I> Console. ReadLine (); 
I>} 
I> 

9

Re: How to declare in a class property on ?

Hello, Muxa, you wrote:

M> and if so?


Then not to address to all remaining fields of a class:




    class MyList 
    { 
      private List <int> list = new List <int> (); 
      public static implicit operator List <int> (MyList l) {return l.list;} 
      
[b]public String Name = "myName";[/b] 
      public MyList () {list. Add (1); list. Add (2); list. Add (3);} 
    }

    static void Main (string [] args) 
    { 
      List <int> myList = (List <int>) new MyList (); 
      Console. WriteLine ( 
[b]myList. Name[/b]); 
    }

10

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:


In C# it not to make.

But there are languages in which it probably.

http://nemerle.org/Design_patterns

11

Re: How to declare in a class property on ?

I> Then not to address to all remaining fields of a class:

Inheritance?




class MyList <T>: List <T> {...} 

12

Re: How to declare in a class property on ?

Hello, Muxa, you wrote:

M> inheritance?


And if the class needs to be inherited from other class?

13

Re: How to declare in a class property on ?

Hello, WolfHound, you wrote:

WH> But there are languages in which it probably.


I know, for example, Delphi5
smile

14

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

WH>> But there are languages in which it probably.
I> I know, for example, Delphi5
smile

And so that at once a little "properties by default"?

15

Re: How to declare in a class property on ?

Hello, WolfHound, you wrote:

WH> And so that at once a little "properties by default"?


... How then to define, what of them most ""?

16

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

I> Hm... How then to define, what of them most ""?

As you would like.

Macroes...

17

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

WH>> And so that at once a little "properties by default"?
I> Hm... How then to define, what of them most ""?


It is offered most to write a macro and most in a macro to decide that is necessary. For example, at the conflict it is possible  an error __. Or the warning and to "pick up" first or not simply "got", and selected on not which strategy by which the macro can be parametrized
smile

In general, both wish, and do and that make that and receive
smile

18

Re: How to declare in a class property on ?

Hello, _FRED _, you wrote:

_FR> It is offered most to write a macro and most in a macro to decide that is necessary. For example, at the conflict it is possible  an error __. Or the warning and to "pick up" first or not simply "got", and selected on not which strategy by which the macro can be parametrized
smile


If does not complicate, result a small example that it was clear as such becomes.

19

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

_FR>> It is offered most to write a macro and most in a macro to decide that is necessary. For example, at the conflict it is possible  an error __. Or the warning and to "pick up" first or not simply "got", and selected on not which strategy by which the macro can be parametrized
smile

I> If does not complicate, result a small example that it was clear as such becomes.


With it it is better to address in forum
.

20

Re: How to declare in a class property on ?

Hello, _FRED _, you wrote:

_FR> With it it is better to address in forum
.


And, so it is other programming language... I and with C# plainly did not understand, and here still something another
smile

21

Re: How to declare in a class property on ?

Hello, ivs13, you wrote:

_FR>> With it it is better to address in forum
.

I> And so it is other programming language... I and with C# plainly did not understand, and here still something another
smile


Apropos  I already told that it is impossible
wink