Skip to main content

C# Specifications Part - 1

C# (pronounced “See Sharp”) is a simple, modern, object-oriented, and type-safe programming language. 
C# has its roots in the C family of languages and will be immediately familiar to C, C++, and Java programmers. 
C# is standardized by ECMA International as the ECMA-334 standard and by ISO/IEC as the ISO/IEC 23270 standard. 
Microsoft’s C# compiler for the .NET Framework is a conforming implementation of both of these standards.

Several C# features aid in the construction of robust and durable applications: Garbage collection automatically reclaims memory occupied by unused objects, 
exception handling provides a structured and extensible approach to error detection and recovery,
and the type-safe design of the language makes it impossible to read from uninitialized variables, 
to index arrays beyond their bounds, or to perform unchecked type casts.

C# has a unified type system. All C# types, including primitive types such as int and double, inherit from a single root object type. 
Thus, all types share a set of common operations, and values of any type can be stored, transported, and operated upon in a consistent manner. 
Furthermore, C# supports both user-defined reference types and value types

Hello world

The “Hello, World” program is traditionally used to introduce a programming language. Here it is in C#:
using System;
class Hello
{
static void Main() {
Console.WriteLine("Hello, World");
}
}
C# source files typically have the file extension .cs. Assuming that the “Hello, World” program is stored in the file hello.cs, the program can be compiled with the Microsoft C# compiler using the command line

csc hello.cs

which produces an executable assembly named hello.exe. The output produced by this application when it is run is

Hello, World

The “Hello, World” program starts with a using directive that references the System namespace. 

Namespaces provide a hierarchical means of organizing C# programs and libraries. 
Namespaces contain types and other namespaces—for example, the System namespace contains a number of types, 
such as the Console class referenced in the program, and a number of other namespaces, such as IO and Collections. 

A using directive that references a given namespace enables unqualified use of the types that are members of that namespace. 
Because of the using directive, the program can use Console.WriteLine as shorthand for System.Console.WriteLine.

The Hello class declared by the “Hello, World” program has a single member, the method named Main. 
The Main method is declared with the static modifier. While instance methods can reference a particular enclosing object instance using the keyword this, 
static methods operate without reference to a particular object. By convention, a static method named Main serves as the entry point of a program.
The output of the program is produced by the WriteLine method of the Console class in the System namespace. 
This class is provided by the .NET Framework class libraries, which, by default, are automatically referenced by the Microsoft C# compiler. 
Note that C# itself does not have a separate runtime library. Instead, the .NET Framework is the runtime library of C#.


Program structure

The key organizational concepts in C# are programs, namespaces, types, members, and assemblies. 
C# programs consist of one or more source files. Programs declare types, which contain members and can be organized into namespaces. 
Classes and interfaces are examples of types. Fields, methods, properties, and events are examples of members. 
When C# programs are compiled, they are physically packaged into assemblies. 
Assemblies typically have the file extension .exe or .dll, depending on whether they implement applications or libraries.

The example

using System;
namespace Acme.Collections
{
public class Stack
{
Entry top;
public void Push(object data) {
top = new Entry(top, data);
}
public object Pop() {
if (top == null) throw new InvalidOperationException();
object result = top.data;
top = top.next;
return result;
}
class Entry
{
public Entry next;
public object data;
public Entry(Entry next, object data) {
this.next = next;
this.data = data;
}
}
}
}
declares a class named Stack in a namespace called Acme.Collections. The fully qualified name of this class is Acme.Collections.Stack. 
The class contains several members: a field named top, two methods named Push and Pop, and a nested class named Entry. 
The Entry class further contains three members: a field named next, a field named data, and a constructor. 
Assuming that the source code of the example is stored in the file acme.cs, the command line

csc /t:library acme.cs

compiles the example as a library (code without a Main entry point) and produces an assembly named acme.dll.
Assemblies contain executable code in the form of Intermediate Language (IL) instructions, 
and symbolic information in the form of metadata. Before it is executed, the IL code in an assembly 
is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime.
Because an assembly is a self-describing unit of functionality containing both code and metadata, 
there is no need for #include directives and header files in C#. The public types and members contained 
in a particular assembly are made available in a C# program simply by referencing that assembly when compiling 
the program. For example, this program uses the Acme.Collections.Stack class from the acme.dll assembly:

using System;
using Acme.Collections;
class Test
{
static void Main() {
Stack s = new Stack();
s.Push(1);
s.Push(10);
s.Push(100);
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}
If the program is stored in the file test.cs, when test.cs is compiled, the acme.dll assembly can be referenced using the compiler’s /r option:

csc /r:acme.dll test.cs

This creates an executable assembly named test.exe, which, when run, produces the output:

100
10
1

C# permits the source text of a program to be stored in several source files. 
When a multi-file C# program is compiled, all of the source files are processed together, 
and the source files can freely reference each other—conceptually, it is as if all the source files 
were concatenated into one large file before being processed. Forward declarations are never needed in C# because, 
with very few exceptions, declaration order is insignificant. 
C# does not limit a source file to declaring only one public type nor does it require the name of the source file to match a type declared in the source file.



Comments

Popular posts from this blog

Accessing File Stored in Windows Azure Blob Storage Using jQuery

Did you know it was possible to access the Windows Azure Blob Storage directly from JavaScript, for example using jQuery? At first, it sounds obvious, since Blobs are after all accessible from a public UR. But in practice, there is a very big hurdle: the Web browser’s Same Origine Policy or SOP, that restricts JavaScript code to accessing resources originating from the same site the script was loaded from. This means that you will never be able to load a Windows Azure Blob using XMLHttpRequest for example! Fortunately, there is a popular workaround called JSONP (“JSON with Padding”). The idea behind this technique is that the script tag is not submitted to the SOP: an HTML page can thus load a JavaScript file from any site. So, if you expose your data in an “executable” form in JavaScript, a page will be able to load this data using a script tag. For example: <script type=”text/javascript” src=”http://www.sandeepknarware.in/exemple.jsonp”> </script> But how can ...

Support for debugging lambda expressions with Visual Studio 2015

Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitati...

gcAllowVeryLargeObjects Element

There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {    ...