C# language

P

p988

When we talk about safe code regarding C#, what do we realy mean? Is safe
code the same as managed code literally?

Why people say that C# is a component-oriented language?

What differences there exist between an event and a method in C#?

What differences there exist between a properties and an attribute in C#?

Why we need to write unsafe code, like inline C code sometimes in C#?
 
P

Paul E Collins

p988 said:
When we talk about safe code regarding C#,
what do we realy mean? Is safe code the same
as managed code literally?

Safe code doesn't involve direct memory manipulation. It doesn't allow the
use of pointers, for example. It's relatively difficult to create a resource
leak using safe code.

Managed code (AFAIK) is any code that uses the .NET libraries, safe or not.
Why people say that C# is a component-
oriented language?

Not sure. I've never heard that term.
What differences there exist between an event
and a method in C#?

A method is basically just a function belonging to a class.

An event is a mechanism for calling a delegate (which calls one or more
methods). Several classes can "sign up" for the event, and their methods
will be called via the delegate when the event occurs. This is less
complicated than it sounds :)
What differences there exist between a properties
and an attribute in C#?

Properties are public data members of a class that usually have 'get' and
'set' functionality. For example, the Form class has a Location property
that specifies where the form appears on the screen.

Attributes in C# have nothing to do with properties. (Perhaps you're
thinking of UML attributes, which are a completely different thing.) They
are used to mark sections of code with a particular meaning, such as
"obsolete" or "single-threaded".
Why we need to write unsafe code, like inline C
code sometimes in C#?

Unsafe code can be useful when you need to traverse a data structure very
quickly (using pointers to memory). A typical C# application will contain
little or no unsafe code, though.

P.
 
J

Joe Mayo

Hi p988,

Please see comments in-line.

Joe
--
http://www.csharp-station.com

p988 said:
When we talk about safe code regarding C#, what do we realy mean? Is safe
code the same as managed code literally?

All code that runs on the .NET CLR is managed. So, C# only produces managed
code. By default, all C# code is safe, which means that the CLR can verify
that it is safe. C# also gives you the ability to produce what is called
"unsafe" code. You identify a region of unsafe code by enclosing it in a
block with the unsafe keyword. Any time you use pointers, you must enclose
them in an unsafe block. The actual operation of the code may in fact be
safe, but because of the way pointers work, there is no way for the CLR to
verify that code with pointers is safe, so it must be placed into its own
block. This forces the developer to be aware that they are using unsafe
code and making them compile the assembly with information indicating that
it contains unsafe code.
Why people say that C# is a component-oriented language?

"Component" is kind of an overloaded term, which adds to the confusion.
However, here is the primary distinction I make between a component and an
object: Components have features, such as interfaces, properties, and
events, that make them reusable entities. While an object could be
reusable, it doesn't necessarily have to be so. Furthermore, multiple
objects may be part of a component. C# is component oriented because it
raises language features that support component oriented development to
first class status.
What differences there exist between an event and a method in C#?

Methods are units of functionality that are executed when called. Events
work on a publish/subscribe model, where code that is interested in an event
can hook up methods to that event (subscribe) and when the event is raised
(published) all methods that were hooked up get called. Think of a Button
that gets pressed. It generates a Click event. If your program has hooked
up a method to that Click event, your method will be called when that Button
is pressed.
What differences there exist between a properties and an attribute in C#?

A property is a type member that enables you to provide encapsulation to
type state with field-like semantics. Attributes serve a totally different
purpose. Attributes allow to decorate parts of your code with declarative
information. Instead of writing code, you can place an attribute in your
code that has pre-defined behavior or is a marker of some type. For
example, using a DllImport attribute allows your program to declare a
prototype that you can use for Win32 calls. Other attributes, such as
Conditional, tell the compiler that it should only compile a method when a
particular directive is defined.
Why we need to write unsafe code, like inline C code sometimes in C#?

If you have a need to use pointers, unsafe code allows you to do so. When
programming in C#, you normally don't ever use unsafe code. I've used it
for the purpose of wrapping P/Invoke calls to unmanaged DLLs, but have
rarely needed or seen a reason to use it otherwise.

Joe
 
D

Daniel Billingsley

Joe Mayo said:
Hi p988,


"Component" is kind of an overloaded term, which adds to the confusion.

I've heard "component-oriented" used in the context of application design,
not for a programming language per se. In the former case it seems to just
mean applying OO concepts at the application level - encapsulation being the
most common idea I find being discussed in regards to business objects.
 
J

JamesMason

p988 said:
When we talk about safe code regarding C#, what do we realy mean? Is safe
code the same as managed code literally?

The term "safe" refers to the memory management model provided by
"managed" code, if the garbage collector is taking care of things your
code can be thought of as "safe" or "managed", however "managed" is
the term used for any executing process that is running under the CLR,
managed code can have "unsafe" blocks of code inside it.

Sound confusing? Okay, here are some examples (e.g. Visual Basic 6
produces only unmanaged code, VB.NET produces only managed code, C#
produces only managed code, Visual C++ 7 can produce unmanaged or
managed code) however C# can mix in "unsafe" code blocks to perform
task where it would be optimal to bypass the automated garbage
collecting.

Why people say that C# is a component-oriented language?

That's not true, C# is an object-oriented language. component-oriented
implies a "glue" language that's at it's strongest when it acts as a
container for components from various lower level languages, Visual
Basic 6 is an excellent example of a component-oriented language.
There's an entire programming mentallity that goes along with
component-oriented programming and that is "buy third-party components
as often as possible and share components between as many applications
as possible), component-oriented programming was a large part of
Windows DNA. For .NET programming in C# and VB.NET you can still use
those ActiveX third-party components, but it's no longer the
recommended solution, because with these new languages you now have a
lot more power to do the lower level functions and you have a really
powerful set of class libraries that make extreemly complex task a lot
simpler in most cases.

C# is much more than a "glue" language, but you can use it as that if
you want io. Classic VB/VB6 is probably better suited for that job.
What differences there exist between an event and a method in C#?

This difference is not unique to C#, take Java2 or classic VB and
you'll find the same type of structure. an event is a message
broadcaster and builds upon the nature of the Windows OS (which is by
design an event driven OS) a method is a encapsulated portion of code
that can be called. The link between the two in C# is known as an
event handler. For example let's say you have a button control when
the user clicks on the button the OS sends that message to the
Framework which recieves it and then the button control raises a .NET
event, event handler(s) can be created to trap the event and send it's
arguments to a specific method call. The program code inside the
method determines what should happen when the event is handled.

This question and the two below it tell me that you're probably a
student looking for some answers. If you do not understand the above
paragraph please do a little research and spend some time building
programs to get a grasp on this.

Check out Cris Sells book on WinForms programming:
http://www.amazon.com/exec/obidos/t...104-4093223-7929569?v=glance&s=books&n=507846
What differences there exist between a properties and an attribute in C#?

Properties refer to encapulated bits of data, exposed from a class.
Attributes in the .NET context usually refer to metadata tags that
work on properties and methods.
Why we need to write unsafe code, like inline C code sometimes in C#?

For more control, C/C++ have pointers which allow you to manipulate
large chunks of memory directly. In order for the managed garbage
collector to do its job this isn't possible in "safe" code, so
Microsoft provided a way that you can declare an "unsafe" code block
in C# and take back that power.


Please do not copy these answers verbatam for academic purposes or
otherwise.

Regards,
James
 
H

Hung Jung Lu

p988 said:
Why people say that C# is a component-oriented language?

To understand components, it's best to see simple GUI (graphical user
interface) examples, since this is one of the first places where
component programming come from. In C# or VB, you can produce a piece
of software in the form of DLL (dynamically-linked library). You can
re-use this DLLs, often in a visual manner. That is, you can
drag-n-drop your ActiveX OCX "component" visually into your program.
That was the ideal dream of component-based programming, and all what
the initial hype of Java beans was about. Before .NET, component
programming in the commercial world basically meant COM (Microsoft) or
CORBA (Java), although Apple MacIntosh was probably the first
well-known system to use component technology. Remember the first time
you were able to cut and paste an image from a paint program into a
word processor document? That's what components are all about: you
have "software components" ("program libraries" in reality) installed
in your system, and you can re-use these components from different
applications. So, magically, a word processor that does not understand
images can all of a sudden have an embedded image displayer. This
library sharing process is later standardized: the libraries must meet
some rigid specifications to facilitate binary communication across
language barriers (C++/VB/etc.) and location barriers (remote
objects). And there you have COM and CORBA. The arrival of .NET brings
a new way of making components, quite different from COM. For
instance, there are no longer registry entries to keep track of the
components. Hence, in the new Windows world, you have to manipulate
both the old COM components and the new .NET components.

C# is a component-oriented language because the language and the IDE
(Integrated Development Environment) help you to use/create
components.
What differences there exist between an event and a method in C#?

That's a good one. Remember how SUN sued Microsoft? Well, one of the
issues was Microsoft "tainted" the Java language by introducing new
features in J++. One of the new features was the introduction of a new
beast called "delegate", unheard of in the Java world. In the Java
world, events and event listeners are separate concepts/objects. What
Microsoft did was some sort of unification, hence in C#, there is one
single beast: "event delegate" to take care of events and event
listeners. Moreover, events are fired by using the () operator,
(that's why your question: event firing looks just like a regular
method!) so Microsoft's "event delegate" actually does three jobs at
once. Is this good or bad? I don't know. I know that when I was doing
Java, it was kind of confusing to juggle all the event-related
objects/methods. So, Microsoft's approach does appear to be a lot more
simplified. This simplification nonetheless could be confusing on its
own. So, in short, an event delegate in C# is: (a) an object to
represent the concept of an event, (b) an event listener queue that
holds references to the event handlers, (c) a callable object for
firing an event. You get three for the price of one. :)
What differences there exist between a properties and an attribute in C#?

Properties are for program objects (as in objects created from a
class), basically they are dynamic data fields, and are used for
normal programming. Attributes are like meta-tags, they are for
meta-programming purposes. They add additional information, which I
guess you should be able to ignore/postpone until you get into
advanced programming, e.g. making .NET components.

regards,

Hung Jung
 
R

Rotes Sapiens

On Thu, 27 Nov 2003 18:19:31 +0000 (UTC), "Paul E Collins"

Also managed code has garbage collection to defragment data in memory.

Safe code doesn't involve direct memory manipulation. It doesn't allow the
use of pointers, for example. It's relatively difficult to create a resource
leak using safe code.

[snip]


Sig:
To be or not to be is true - G. Boole
 

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