C# newbie questions

D

Debaser

Hi all. I bought the Dietel book "C# for programmers" and it's quite
encompassing. I've learned much....however....
I have some questions left (please answer them all if you can...each
is as important)

What is the differece between OVERRIDE and NEW (caps to indicate
keywords)

I sent a project I made to a friend to test. Having VS2005, I have the
..NET 2.0 FW installed. They did not (1.1 I believe). Should/Can I
downgrade the compatibility? Or is this problem from my code? Is it
adjustable in the IDE?

Would someone explain to my in the simplest of words (like a C# for
Dummies book would) what a delegate method/function is?


Thanks,
Mike
 
J

Jon Skeet [C# MVP]

Debaser said:
Hi all. I bought the Dietel book "C# for programmers" and it's quite
encompassing. I've learned much....however....
I have some questions left (please answer them all if you can...each
is as important)

What is the differece between OVERRIDE and NEW (caps to indicate
keywords)

See http://www.pobox.com/~skeet/csharp/faq/#override.new
I sent a project I made to a friend to test. Having VS2005, I have the
.NET 2.0 FW installed. They did not (1.1 I believe). Should/Can I
downgrade the compatibility? Or is this problem from my code? Is it
adjustable in the IDE?

You can't easily make VS 2005 compile for 1.1. MS are working on this
(Google for "MS BEE"). Whether you *would* be able to compile your code
for 1.1 depends on what the code uses - if it uses generics, for
instance, you would have to rewrite those sections. There are some
elements of the framework which are new, and some language elements
which are new.
Would someone explain to my in the simplest of words (like a C# for
Dummies book would) what a delegate method/function is?

There are two things to talk about here. One is a delegate type, which
is pretty much just a set of parameter types and a return type, given a
name. For instance:

delegate string Foo (int x, double y);

declares a delegate type named "Foo" which represents a method which
takes an int and a double, and returns a string.

Now, an *instance* of a delegate type is created by providing enough
information for a method to be executed - usually, the method within a
normal type, and an instance of the type if it's an instance method.
(If it's a static method, there's no need.) Note that the same delegate
type can be instantiated using a static method or an instance method.

Instances of the delegate can be passed around (or rather, references
are passed around - delegates are reference types) and maintain their
type safety. You can then "call" the delegate which really means
calling the method it was instantiated for, with the appropriate
context.

I think an example would help. Hopefully it'll clear things up, but do
come back to me on this if you still have problems.

using System;

delegate string Foo (int x, double y);

public class Test
{
int i;

public Test(int a)
{
i = a;
}

public string MultiplyByI(int x, double y)
{
return string.Format ("{0}, {1}", x*i, y*i);
}

public static string MultiplyByPi (int x, double y)
{
return string.Format ("{0}, {1}", x*Math.PI, y*Math.PI);
}


static void Main()
{
Test first = new Test(2);
Test second = new Test(10);

// Use the method MultiplyByI with the "target"
// context of "first"
Foo firstDelegate = new Foo(first.MultiplyByI);

// Use the method MultiplyByI with the "target"
// context of "second"
Foo secondDelegate = new Foo(second.MultiplyByI);

// Use the method MultiplyByPi with no "target"
// context - it's a static method
Foo thirdDelegate = new Foo(MultiplyByPi);

// Now execute each one in turn and display the result
Display (firstDelegate);
Display (secondDelegate);
Display (thirdDelegate);
}

static void Display (Foo delegateInstance)
{
Console.WriteLine (delegateInstance (1, 3.4));
}
}
 
J

Jon Shemitz

"Jon Skeet [C# MVP]" answered this question:

Patiently. Well. After I said, oh, no, I have work to do.
 
C

clintonG

Well Mike,
Insights from fellas like Jon Skeet are very helpful but I'd like to
recommend another book at this point.

"C# Class Design Handbook" published by WROX.
Ask Barnes and Noble to bring it into your closest store for review.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/


Jon Shemitz said:
"Jon Skeet [C# MVP]" answered this question:

Patiently. Well. After I said, oh, no, I have work to do.
 

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

Similar Threads

i want to learn c# 1
C# Questions 9
difference c# 2.0 C# 3.0 4
Wonderful opinionated book on C# 16
C# projects 2
C# design patterns 2
C# Newbie 5
Couple of newbie questions... 2

Top