3 explanations required - please help

A

almurph

Hi everyone,


I'm wondering can you help me understanding some C# syntax.


1. What does the this do? specifically the <string> thingy?

List<string> keys = new List<string>();



2. When you preceed a class with "[Serializable()]" - what does it
mean/do?:

[Serializable()]
public class myclass : ISerializable
{
//implementation
}



3. Brackes around a "Cannot implicitely convert SomeWrapperClass to
SomeWrapperClass[] - what does this do?


I get a "cannot implicitly convert SomeWrapperClass to
SomeWrapperClass[]" from the following line of code:


SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };


however when I put braces around it, it works:


SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };


Does the placing of brackets around something allow implicit
conversions or something?



Any comments/suggestions/explanations of the above much appreciated.
Thanks,
Al.
 
J

Jon Skeet [C# MVP]

I'm wondering can you help me understanding some C# syntax.


1. What does the this do? specifically the <string> thingy?

List<string> keys = new List<string>();

That shows it's a list "of strings". This feature is called generics.

Part of an explanation:
http://www.yoda.arachsys.com/csharp/csharp2/generics.html

There's much more on the web.
2. When you preceed a class with "[Serializable()]" - what does it
mean/do?:

[Serializable()]
public class myclass : ISerializable
{
//implementation
}

That means the class has the SerializableAttribute applied to it, which
means you can serialize it. Serialization is like freeze-drying an
instance of a class, so you can then restore it later (or somewhere
else).
3. Brackes around a "Cannot implicitely convert SomeWrapperClass to
SomeWrapperClass[] - what does this do?


I get a "cannot implicitly convert SomeWrapperClass to
SomeWrapperClass[]" from the following line of code:


SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };


however when I put braces around it, it works:


SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };

I think you meant to not put the braces in the first bit of code...
Does the placing of brackets around something allow implicit
conversions or something?

No - it makes it an array initializer. For instance:

int[] i = { 1, 2, 3 }; // Fine
int[] i = 1, 2, 3; // Not okay

Note that array initializers like this are only allows with variable
declarations. Otherwise you have to do:

int[] i;
....
i = new int[] { 1, 2, 3 };

With C# 3 you can use type inference:

int[] i;
....
i = new[] { 1, 2, 3 };
 
C

Cowboy \(Gregory A. Beamer\)

Question #1:

List<T> is a generic. What it does is give you the ability to strongly type
a list without writing specific code for each implementation.

In the old list, everythign was an object. The below is not designed to be
viable syntax, but simply to express the difference generics make:

//string
List list = new List();
list.Add("first value");
list.Add("second value");

string s = list[0].ToString();
string s2 = (string) list[1];

//but with int
List list2 = new List();
list2.Add(1)
int i = (int) list[0];

With Generics, the underlying generic type List<T> can be of any type, so
you now change the implementations to:

List<string> list = new List<string>();
list.Add("first value");
string s = list[0];

List<int> list2 = new List<int>();
list.Add(1);
int i = list[0];

Much easier. In addition, you can avoid many errors, as the compiler will
catch you adding a string to an int list, etc.

Question 2:
Serializable means that an object can serialize. What this means, in less
technical terms, is you can serve the object up through a marshalling
interface, like a web service, and reconstitute it on the other end. This is
very useful when you are using things like web services, but there are other
reasons to serialize.

Question 3:
SomeWrapperClass[] is an array, so you would instantiate like so:

SomeWrapperClass[] = new SomeWrapperClass[1] { new
SomeWrapperClass("@itemValue",
itemKey) };

This would mean that you have an array of SomeWrapperClass with a single
item. I would aim for a list in thsi case, like List<SomeWrapperClass>
personally. You may also have to use a SortedList, if a certain order is
needed.

Hope this helps!

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

*************************************************
| Think outside the box!
|
*************************************************
Hi everyone,


I'm wondering can you help me understanding some C# syntax.


1. What does the this do? specifically the <string> thingy?

List<string> keys = new List<string>();



2. When you preceed a class with "[Serializable()]" - what does it
mean/do?:

[Serializable()]
public class myclass : ISerializable
{
//implementation
}



3. Brackes around a "Cannot implicitely convert SomeWrapperClass to
SomeWrapperClass[] - what does this do?


I get a "cannot implicitly convert SomeWrapperClass to
SomeWrapperClass[]" from the following line of code:


SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };


however when I put braces around it, it works:


SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };


Does the placing of brackets around something allow implicit
conversions or something?



Any comments/suggestions/explanations of the above much appreciated.
Thanks,
Al.
 
A

almurph

Question #1:

List<T> is a generic. What it does is give you the ability to strongly type
a list without writing specific code for each implementation.

In the old list, everythign was an object. The below is not designed to be
viable syntax, but simply to express the difference generics make:

//string
List list = new List();
list.Add("first value");
list.Add("second value");

string s = list[0].ToString();
string s2 = (string) list[1];

//but with int
List list2 = new List();
list2.Add(1)
int i = (int) list[0];

With Generics, the underlying generic type List<T> can be of any type, so
you now change the implementations to:

List<string> list = new List<string>();
list.Add("first value");
string s = list[0];

List<int> list2 = new List<int>();
list.Add(1);
int i = list[0];

Much easier. In addition, you can avoid many errors, as the compiler will
catch you adding a string to an int list, etc.

Question 2:
Serializable means that an object can serialize. What this means, in less
technical terms, is you can serve the object up through a marshalling
interface, like a web service, and reconstitute it on the other end. This is
very useful when you are using things like web services, but there are other
reasons to serialize.

Question 3:
SomeWrapperClass[] is an array, so you would instantiate like so:

SomeWrapperClass[] = new SomeWrapperClass[1] { new
SomeWrapperClass("@itemValue",
itemKey) };

This would mean that you have an array of SomeWrapperClass with a single
item. I would aim for a list in thsi case, like List<SomeWrapperClass>
personally. You may also have to use a SortedList, if a certain order is
needed.

Hope this helps!

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my bloghttp://gregorybeamer.spaces.live.com/lists/feed.rss

*************************************************
| Think outside the box!
|



Hi everyone,
I'm wondering can you help me understanding some C# syntax.
1. What does the this do? specifically the <string> thingy?
List<string> keys = new List<string>();
2. When you preceed a class with "[Serializable()]" - what does it
mean/do?:
[Serializable()]
public class myclass : ISerializable
{
//implementation
}
3. Brackes around a "Cannot implicitely convert SomeWrapperClass to
SomeWrapperClass[] - what does this do?
I get a "cannot implicitly convert SomeWrapperClass to
SomeWrapperClass[]" from the following line of code:
SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };
however when I put  braces around it, it works:
SomeWrapperClass[] parameters = { new SomeWrapperClass("@itemValue",
itemKey) };
Does the placing of brackets around something allow implicit
conversions or something?
Any comments/suggestions/explanations of the above much appreciated.
Thanks,
Al.- Hide quoted text -

- Show quoted text -

Thank you both very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top