Home > Articles > Programming > C#

Why Another Programming Language?

A good question that has to be answered is why you should learn another programming language when you are already doing enterprise development in C++ or Visual Basic. The marketing-type answer is that "C# is intended to be the premier language for writing .NET applications in the enterprise computing space." This chapter is about backing up that claim with arguments, and showcasing a slew of C#'s features. This chapter is about whetting your appetite.

The programming language C# derives from C and C++; however, it is modern, simple, entirely object-oriented, and type-safe. If you are a C/C++ programmer, your learning curve will be flat. Many C# statements are directly borrowed from your favorite language, including expressions and operators. If you don't look too closely at first, a C# program looks like a C++ program.

An important point about C# is that it is a modern programming language. It simplifies and modernizes C++ in the areas of classes, namespaces, method overloading, and exception handling. Much of the complexity of C++ was removed from C# to make it easier to use and less error prone.

Contributing to the ease of use is the elimination of certain features of C++: no more macros, no templates, and no multiple inheritance. The aforementioned features create more problems than they provide benefit—especially for enterprise developers.

New features for added convenience are strict type safety, versioning, garbage collection, and many more. All these features are targeted at developing component-oriented software. Although you don't have the sheer power of C++, you become more productive faster.

Before I get ahead of myself and present too many features, I want to stop and present the various elements of C# based on key points in the following sections:

  • Simple
  • Modern
  • Object-oriented
  • Type-safe
  • Versionable
  • Compatible
  • Flexible

Simple

One thing you definitely wouldn't attribute to C++ is that learning it is simple. This is not so with C#. The foremost goal for this programming language was simplicity. Many features—or the lack thereof—contribute to the overall simplicity of C#.

Pointers are a prominent feature that is missing in C#. By default, you are working with managed code, where unsafe operations, such as direct memory manipulation, are not allowed. I don't think any C++ programmer can claim never to have accessed memory that didn't belong to him via a pointer.

Closely related to the pointer "drama" is operator "madness." In C++, you have ::, ., and -> operators that are used for namespaces, members, and references. For a beginner, operators make for yet another hard day of learning. C# does away with the different operators in favor of a single one: the . (the "dot"). All that a programmer now has to understand is the notion of nested names.

You no longer have to remember cryptic types that originate from different processor architectures—including varying ranges of integer types. C# does away with them by providing a unified type system. This type system enables you to view every type as an object, be it a primitive type or a full-blown class. In contrast to other programming languages, treating a simple type as an object does not come with a performance penalty because of a mechanism called boxing and unboxing. Boxing and unboxing is explained later in detail, but basically, this technique provides object access to simple types only when requested.

At first, seasoned programmers might not like it, but integer and Boolean data types are now finally two entirely different types. That means a mistaken assignment in an if statement is now flagged as an error by the compiler because it takes a Boolean result only. No more comparison-versus-assignment errors!

C# also gets rid of redundancies that crept into C++ over the years. Such redundancies include, for example, const versus #define, different character types, and soon. Commonly used forms are available in C#, whereas the redundant forms were eliminated from the language.

Modern

The effort you put into learning C# is a great investment because C# was designed to be the premier language for writing .NET applications. You will find many features that you had to implement yourself in C++, or that were simply unavailable, are part of the base C# language implementation.

The financial types are a welcome addition for an enterprise-level programming language. You get a new decimal data type that is targeted at monetary calculations. If you are not fond of the provided simple types, you can easily create new ones specifically crafted for your application.

I have already mentioned that pointers are no longer part of your programming weaponry. You won't be too surprised then that the entire memory management is no longer your duty—the Common Language Runtime (CLR) provides a garbage collector that is responsible for memory management in your C# programs. Because memory and your application are managed, it is imperative that type safety be enforced to guarantee application stability.

It is not exactly news to C++ programmers, but exception handling is a main feature of C#. The difference from C++, however, is that exception handling is cross-language (another feature of the runtime).Prior to C#, you had to deal with quirky HRESULTs—this is now over because of robust error handling that is based on exceptions.

Security is a top requirement for a modern application. C# won't leave you alone on this either: It provides metadata syntax for declaring capabilities and permissions for the underlying .NET security model. Metadata is a key concept of the CLR, and the next chapter deals with its implications in more depth.

Object-Oriented

You wouldn't expect a new language to not support object-oriented features, would you? C#, of course, supports all the key object-oriented concepts such as encapsulation, inheritance, and polymorphism. The entire C# class model is built on top of the CLR's Virtual Object System(VOS), which is described in the next chapter. The object model is part of the infrastructure, and is no longer part of the programming language.

One thing you will notice right from the start is that there are no more global functions, variables, or constants. Everything must be encapsulated inside a class, either as an instance member (accessible via an instance of a class—an object) or a static member (via the type). This makes your C# code more readable and also helps to reduce potential naming conflicts.

The methods you can define on classes are, by default, non virtual (they cannot be overridden by deriving classes). The main point of this is that another source of errors disappears—the accidental overriding of methods. For a method to be able to be overridden, it must have the explicit virtual modifier. This behavior not only reduces the size of the virtual function table, but also guarantees correct versioning behavior.

When you are used to programming classes in C++, you know that you can set different access levels for class members by using access modifiers. C# also supports the private, protected, and public access modifiers, and also adds a fourth one: internal. Details about these access modifiers are presented in chapter 5, "Classes."

How many of you have ever created a class that derives from multiple base classes? (ATL programmers, your vote doesn't count!) In most cases, you need to derive from only one class. Multiple base classes usually add more problems than they solve. That is why C# allows only one base class. If you feel the need for multiple inheritance, you can implement interfaces.

A question that might come up is how to emulate function pointers when there are no pointers in C#. The answer to this question is delegates, which provide the underpinnings for the CLR's event model. Again, I have to put off a full explanation until chapter 5.

Type-Safe

Once again, I have to pick on pointers as an example. When you had a pointer in C++, you were free to cast it to any type, including doing rather idiotic things such as casting an int* (int pointer) to a double* (double pointer). As long as memory backed that operation, it kind of "worked." This is not the kind of type safety you would envision for an enterprise-level programming language.

Because of the outlined problems, C# implements strictest type safety to protect itself and the garbage collector. Therefore, you must abide by a few rules in C# with regard to variables:

  • You cannot use uninitialized variables. For member variables of an object, the compiler takes care of zeroing them. For local variables, you are incharge. However, if you use an uninitialized variable, the compiler will tell you so. The advantage is that you get rid of those errors when using an uninitialized variable to compute a result and you don't know how these funny results are produced.
  • C# does away with unsafe casts. You cannot cast from an integer to a reference type (object, for example), and when you downcast, C# verifies that this cast is okay. (That is, that the derived object is really derived from the class to which you are down casting it.)
  • Bounds checking is part of C#. It is no longer possible to use that "extra" array element n, when the array actually has n-1 elements. This makes it impossible to overwrite unallocated memory.
  • Arithmetic operations could overflow the range of the result data type. C# allows you to check for overflow in such operations on either an application level or a statement level. With overflow checking enabled, an exception is thrown when an overflow happens.
  • Reference parameters that are passed in C# are type-safe.

Versionable

Over the past few years, almost every programmer has had to deal at least once with what has become known as "DLL Hell." The problem stems from the fact that multiple applications install different versions of the same DLL to the computer. Sometimes, older applications happily work with the newer version of the DLL; however, most of the time, they break. Versioning is a real pain today.

As you will see in chapter 8, "Writing Components in C#," the versioning support for applications you write is provided by the CLR. C# does its best to support this versioning. Although C# itself cannot guarantee correct versioning, it can ensure that versioning is possible for the programmer. With this support in place, a developer can guarantee that as his class library evolves, it will retain binary compatibility with existing client applications.

Compatible

C# does not live in a closed world. It allows you access to different APIs, with the foremost being the .NET Common Language Specification (CLS). The CLS defines a standard for interoperation between languages that adhere to this standard. To enforce CLS compliance, the compiler of C# checks that all publicly exported entities comply, and raises an error if they do not.

Of course, you also want to be able to access your older COM objects. The CLR provides transparent access to COM. Integration with legacy code is presented in chapter 10, "Interoperating with Unmanaged Code."

OLE Automation is a special kind of animal. Anyone who ever created an OLE Automation project in C++ will have come to love the various Automation data types. The good news is that C# supports them, without bothering you with details.

Finally, C# enables you to inter operate with C-style APIs. Any entry point in a DLL—given its C-styledness—is accessible from your applications. This feature for accessing native APIs is called Platform Invocation Services (PInvoke), and chapter 10 shows a few examples of inter operating with C APIs.

Flexible

The last paragraph of the previous section might have raised an alert with C programmers. You might ask, "Aren't there APIs to which I have to pass a pointer?" You are right. There are not only a few such APIs, but quite a large number (a small understatement). This access to native WIN32 code sometimes makes using unsafe classic pointers mandatory (although some of it can be handled by the support of COM and PInvoke).

Although the default for C# code is safe mode, you can declare certain classes or only methods of classes to be unsafe. This declaration enables you to use pointers, structs, and statically allocated arrays. Both safe code and unsafe code run in the managed space, which implies that no marshaling is incurred when calling unsafe code from safe code.

What are the implications of dealing with your own memory in unsafemode? Well, the garbage collector, of course, may not touch your memory locations and move them just as it does for managed code. Unsafe variables are pinned into the memory block managed by the garbage collector.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020