Ads 468x60px

.

Language Fundamentals in C#

Constants & Variables
A variable is a named memory location. They are programming elements that can change during program execution. Data that needs to be stored in memory & accessed at a later time are stored in variables. Instead of referring to the memory location by the actual memory address you refer to it with a variable name.

Variables are declared as follows
            int a;
They can also be initialized at the time of declaration as follows:
            nt a = 10;
Constants are very similar to variables. The main difference is that the value contained in memory cannot be changed once the constant is declared. When you declare a constant its value is also specified and this value cannot be changed during program execution. Constants are used in situations where we need to keep the value in some memory location constant. If you use hard-coded values, and the value is changed then it has to be changed in all the locations in the code where it has been used. Instead if we are using constants, all we will need to do is to change the value of the constant. This would propagate the changes to our entire application. Constants are declared as follows
            const int a;

Simple Types (Primitive Data types)
Simple or value type variables are those, which are assigned space in the stack instead of the heap. All the primitive types such as int, double etc are value type variables. The simple types basically consist of Boolean and Numeric types, where Numeric is further divided into Integral and Floating Point. The first rule of value types is that they cannot be null. Anytime you declare a variable of value type, you have allocated the number of bytes associated with that type on the stack and are working directly with that allocated array of bits. In addition, when you pass a variable of value type, you are passing that variable’s value and not a reference to the underlying object.

Object Type
Object type or reference type variables are those, which are allocated storage space in the heap. Reference type objects can be null. When a reference type is allocated under the covers a value is allocated on the heap and a reference to that value is returned. There are basically four reference types: classes, interfaces, delegates and arrays.

Class Type
Custom data types are available in .NET framework in the form of classes or class type. It is nothing but a set of data and related behavior that is defined by the developer. Object type and class type are both reference type variables. The only difference comes from the fact that object type consists of objects predefined and available with the .NET framework such as string whereas class type consists of custom user defined data types such as the class employee given below.

CODES:

Overloading and Overriding of the Class
Overloading provides the ability to create multiple methods or properties with the same name, but with different parameters lists. This is a feature of polymorphism. A simple example would be an addition function, which will add the numbers if two integer parameters are passed to it and concatenate the strings if two strings are passed to it.
CODES:

History of C#

.NET framework offers a myriad of languages which puts us programmers into a deep thought process about which programming language best suits our needs. Which language is the "best" language choice? If you are a VB wizard, should you take the time to learn C# or continue to use VB.NET? Are C# ASP.NET pages "faster" than VB .NET ASP.NET pages? These are questions that you may find yourself asking, especially when you're just starting to delve into .NET. Fortunately the answer is simple: there is no "best" language. All .NET languages use, at their root, functionality from the set of classes provided by the .NET Framework. Therefore, everything you can do in VB.NET you can do in C#, and vice-a-versa.

The differences occur in three main areas: syntax, object-oriented principles, and the Visual Studio .NET IDE. Syntax concerns the statements and language elements. Object Oriented differences are less obvious, and concern differences in implementation and feature sets between the two languages. IDE differences include things like compiler settings or attributes. There is also a fourth area of difference: language features that are present in one language but have no equivalent in the other. If you are more familiar with Java, JScript, or C/C++, you may find C#'s syntax more familiar than VB.NET's.

A good question that has to be answered in order to keep you interested in learning C# is Why should you learn another programming language when you already doing enterprise development in C++ or Java. The very answer at the first go will be C# is intended to be the premier language for writing NGWS (Next Generation of Windows Services) applications in the enterprise computing world.

The programming language C# derives from C and C++; however apart from being entirely object oriented it is type safe and simple too. If you are a C/C++ programmer your learning curve should be flat. Many C# statements including expressions and operators have been taken directly taken from your favourite language An important point about C# is that it simplifies and modernizes C++ in the areas of classes, namespaces and exception handling. Much of complex features have not been included or in fact hidden in C# to make it easer to use and more importantly less error prone for e.g. no more macros, templates and no multiple inheritances (Few of you might not like it.) C# provides you with convenient features like garbage collection, versioning and lot more.

The only expense that I can think of is that your code operates in safe mode, where no pointers are allowed. However if you want to use pointers you are not restricted from using it via unsafe code- and no marshalling is involved when calling the unsafe code. So you will learn a great deal of this new language in the coming Sections and see for yourself that how C# resembles or not resembles your favorite language

3.5 Summary

Over the course of topics covered in this session you have seen how to create a simple HelloWorld program, to know the internals of the .NET Framework Runtime. For further understanding or clarification you can always use the .NET framework SDK help documentation and MSDN online.