More basic questions

P

p988

Learning C# is much tougher than I expected...Please help me by answering
the following questions! Thank you in advance!


1. Are all Enumerations type Value type?

2. The line,

RegistryKey Reg = new RegistryKey();

gives the error: "No overload for method 'RegistryKey' takes '0' arguments."

After changing it to:

RegistryKey Reg;

everything OK.

Why?


3. Given a struct:

struct Point
{
public int X;
public int Y;
}

If Point P = new Point;

compiler gives an error: "A new expression requires () or [] after type"

In C++, no difference between "Point P = new Point;" and "Point P = new
Point();"

Guess in C#, have to use: "Point P = new Point();"

Is this a correct understanding?



4. "For example, they (structs) are sealed, which means they cannot be
derived from or have any base class other than System.ValueType, which is
derived from Object."

interface IPoint
{
void SetX (int x);
void SetY (Y);
}

struct sPoint : IPoint
{
...
}

Can a struct, sPoint, be derived from IPoint at all?
If "can", will all instances of sPoint are Value type?


5. Given the code below:

interface IPoint
{
void SetX (int x);
void SetY (Y);
}

class cPoint : IPoint
{
...
}

Are all instances of cPoint Reference type automatically?



6. "Structs cannot declare a default (parameterless) constructor."

Is it legal to write a struct like:

struct Point
{
public Point(int a, int b) { X = a; Y = b; }
int X;
int Y;
}


7. A book says NET Framework Data Types can mean any of the following:
Classes, Structs, Interfaces, Enumerations,Delegates.

Why is the primitive type like int not on the list?


8. Is the following code legal in C#?

int a;
a = 123;
Console.WriteLine(a.ToString())

At least, IntelliSense of VS .NET tells me "a" has no member of "ToString().
Not sure...


9. Since everything in C# is derived from Object, then from what is "CPoint"
derived below?

class CPoint
{
public int X;
}
 
V

Vijaye Raji

Inline...

p988 said:
Learning C# is much tougher than I expected...Please help me by answering
the following questions! Thank you in advance!


1. Are all Enumerations type Value type? VJ: Yes.

2. The line,

RegistryKey Reg = new RegistryKey();

gives the error: "No overload for method 'RegistryKey' takes '0' arguments."

After changing it to:

RegistryKey Reg;

everything OK.

VJ: Registry key class doesn't have an exposed Constructor. If you want to
read or write values, you use the Static members of class Registry, viz.
ClassesRoot, CurrentUser, etc.
Why?


3. Given a struct:

struct Point
{
public int X;
public int Y;
}

If Point P = new Point;

compiler gives an error: "A new expression requires () or [] after type"

In C++, no difference between "Point P = new Point;" and "Point P = new
Point();"

Guess in C#, have to use: "Point P = new Point();"

Is this a correct understanding?
VJ: Yes. You have to invoke the constructor - even if it's the default
constructor, although Point doesn't have a default constructor - which
means, you cannot do
Point P = new Point();

You have to do, Point P = new Point(x, y);
4. "For example, they (structs) are sealed, which means they cannot be
derived from or have any base class other than System.ValueType, which is
derived from Object."

interface IPoint
{
void SetX (int x);
void SetY (Y);
}

struct sPoint : IPoint
{
...
}

Can a struct, sPoint, be derived from IPoint at all?
If "can", will all instances of sPoint are Value type?
VJ: Yes. Structs can implement interfaces and all structs are value types.
5. Given the code below:

interface IPoint
{
void SetX (int x);
void SetY (Y);
}

class cPoint : IPoint
{
...
}

Are all instances of cPoint Reference type automatically?

VJ: Yes.

6. "Structs cannot declare a default (parameterless) constructor."

Is it legal to write a struct like:

struct Point
{
public Point(int a, int b) { X = a; Y = b; }
int X;
int Y;
}
VJ: Structures *can* have default constructors.
7. A book says NET Framework Data Types can mean any of the following:
Classes, Structs, Interfaces, Enumerations,Delegates.

Why is the primitive type like int not on the list?
VJ: I'm not sure. I guess, they're not talking about C# in particular. In
..Net there's no int primitive type. It's a class Int32, Int16, etc.
8. Is the following code legal in C#?

int a;
a = 123;
Console.WriteLine(a.ToString())

At least, IntelliSense of VS .NET tells me "a" has no member of "ToString().
Not sure...
VJ: Yes. This is legal. All objects expose "ToString()" method.
 
T

Tom Shelton

Learning C# is much tougher than I expected...Please help me by answering
the following questions! Thank you in advance!


1. Are all Enumerations type Value type?

Yes. Since System.Enum inherits from System.ValueType.
2. The line,

RegistryKey Reg = new RegistryKey();

gives the error: "No overload for method 'RegistryKey' takes '0' arguments."

After changing it to:

RegistryKey Reg;

everything OK.

Why?

RegistryKey has no publicly accesible constructors. To get an instance
of a RegistryKey you are required to call methods of other classes...
Such as the Registry class. Look in the documentation for
Microsoft.Win32 namespace.
3. Given a struct:

struct Point
{
public int X;
public int Y;
}

If Point P = new Point;

compiler gives an error: "A new expression requires () or [] after type"

In C++, no difference between "Point P = new Point;" and "Point P = new
Point();"

Guess in C#, have to use: "Point P = new Point();"

Is this a correct understanding?

Yep.



4. "For example, they (structs) are sealed, which means they cannot be
derived from or have any base class other than System.ValueType, which is
derived from Object."

interface IPoint
{
void SetX (int x);
void SetY (Y);
}

struct sPoint : IPoint
{
...
}

Can a struct, sPoint, be derived from IPoint at all?
If "can", will all instances of sPoint are Value type?

struct's can implement interfaces - they just can't derive from other
objects. And yes, sPoint instances will still be value types.
5. Given the code below:

interface IPoint
{
void SetX (int x);
void SetY (Y);
}

class cPoint : IPoint
{
...
}

Are all instances of cPoint Reference type automatically?

Yes. Because they are classes.
6. "Structs cannot declare a default (parameterless) constructor."

Is it legal to write a struct like:

struct Point
{
public Point(int a, int b) { X = a; Y = b; }
int X;
int Y;
}

No, that is fine. You just can't implement a default constructor. I do
it all the time for P/Invoke calls (about the only place I usually have
a call to actually use a struct).
7. A book says NET Framework Data Types can mean any of the following:
Classes, Structs, Interfaces, Enumerations,Delegates.

Why is the primitive type like int not on the list?

int is a struct. C# just maps int to the System.Int32 structure. This
is true of all the C# types.
8. Is the following code legal in C#?

int a;
a = 123;
Console.WriteLine(a.ToString())

At least, IntelliSense of VS .NET tells me "a" has no member of "ToString().
Not sure...

Yes. It's legal. So is:

Console.WriteLine(123.ToString());

Not sure why it doesn't show up in the IntelliSense though...
9. Since everything in C# is derived from Object, then from what is "CPoint"
derived below?

class CPoint
{
public int X;
}

Uh, didn't you just answer that? Every thing is derived from Object...
Basically, there is an implicit addition of Object to it's base class
list... So, the runtime is really seeing:

class CPoint : Object
{
public int X;
public int Y;
}
 
J

Jon Skeet [C# MVP]

Vijaye Raji said:
3. Given a struct:

struct Point
{
public int X;
public int Y;
}

If Point P = new Point;

compiler gives an error: "A new expression requires () or [] after type"

In C++, no difference between "Point P = new Point;" and "Point P = new
Point();"

Guess in C#, have to use: "Point P = new Point();"

Is this a correct understanding?
VJ: Yes. You have to invoke the constructor - even if it's the default
constructor, although Point doesn't have a default constructor - which
means, you cannot do
Point P = new Point();

You have to do, Point P = new Point(x, y);

No you don't. *All* value types have a parameterless constructor, but
you can't define your own one. So the following *does* compile:

using System;

struct Point
{
public int X;
public int Y;
}


class Test
{
static void Main(string[] args)
{
Point p = new Point();
}
}
 

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