473,493 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

More basic questions

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;
}

Nov 15 '05 #1
3 1423
Inline...

"p988" <p9**@hotmail.com> wrote in message
news:eS**************@TK2MSFTNGP11.phx.gbl...
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.
9. Since everything in C# is derived from Object, then from what is "CPoint" derived below?

class CPoint
{
public int X;
}

VJ: object, of course!

Nov 15 '05 #2
On 2003-11-26, p988 <p9**@hotmail.com> wrote:
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;
}

--
Tom Shelton
MVP [Visual Basic]
Nov 15 '05 #3
Vijaye Raji <no************@hotmail.com> wrote:
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();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

2
4212
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
2
5166
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
4
2214
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
3
1662
by: Jim H | last post by:
If there is a site someone can point me to that answers such basic questions, rather than taking up support's time posting answers, please let me know. I've developed C# .NET, unmanaged C++, and...
5
1222
by: A. Gaubatz | last post by:
In VB6, I would open files with: Open <file name> For <input/output/append> As <file number> I could then use the Write/Input commands to modify/read the file. How do I do this in VB.NET? ...
12
1624
by: Mark P | last post by:
A couple template related questions: 1. In the code pasted below, why does the basic version get selected in the call to foo and the const version get selected in the call to bar? 2. When...
0
541
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
4
2624
by: Goran Djuranovic | last post by:
Hi all, I am experiencing a strange thing happening with a "designer.vb" page. Controls I manually declare in this page are automatically deleted after I drop another control on a ".aspx" page. -...
151
7905
by: istillshine | last post by:
There are many languages around: C++, JAVA, PASCAL, and so on. I tried to learn C++ and JAVA, but ended up criticizing them. Is it because C was my first programming language? I like C...
0
6989
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...
0
7157
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,...
0
7195
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...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
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...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.