Login Register


The C# Programming Language Introduction filter_list
Author
Message
The C# Programming Language Introduction #1
[Image: V9JJ9Ag.png]

Topics Covered:
Code:
• Overview.................................... 1.1 • .NET internals.............................. 2.1 - MSIL and JIT:........................... 2.2 - Ngen Utility & "pre-JIT'ing".............2.3 - Dynamic Language Runtime:............... 2.4 • Basic Questions/Answers FAQ:................ 3.1 • Features:................................... 4.1 • Specification:.............................. 5.1 • Requirements:............................... 6.1 • .NET Framework Information.................. 7.1 • Sample Code:................................ 8.1

• 1.1 - Overview:
The C# language is a programming language in which was designed to build applications that run on the .NET framework. It comes with type safety, support for OOP, and furthermore, exception handling, garbage collection, etc... through the CLR, among many other things that make this language very enjoyable (I say this without sarcasm). The best thing in my opinion about C# is that it allows for fast development, while giving the feel of other C style languages by syntax (C/C++), as well as Java (even though I don't have much experience with Java, it has to be mentioned here).

It was also one of the first languages to have support for the XNA framework, before VB.net was included later on. Another thing I like about C# is that it allows you to develop games through Unity if you wanted to skip by the XNA framework.

Most recently introduced in VS 2012 was the Async and Await phenomena, which makes asynchronous coding much easier... Previously, you had to define callbacks of your own to define what happens after an asynchronous task had completed, with the await and async keywords, most of this is already done for you. (Information here: http://msdn.microsoft.com/en-us/library/...91443.aspx)

If you are interested in some general sample applications, they can be downloaded here: http://msdn.microsoft.com/en-us/library/...sy596.aspx


• 2.1 - .NET internals:
  • 2.2 - MSIL and JIT:
    When you compile code that uses the .NET framework, you aren't building a native compiled binary obviously. Compiling a program in C# .NET compiles your code into MSIL code (or CIL--Common Intermediate Language), and of which is not specific to any OS, as bytecode is not specific to any OS, nor C# as a language itself. So, this means that every time you run your application the JIT compiler has to take this MSIL code and convert it into native code that your OS can finally run, and the processor can understand. (NOTE: There's ways to pre-JIT, but that's for an advanced topic maybe later.) One disadvantage of this obviously is that .NET applications will use a bit more memory having to go through the JIT compilation process.

    The following describes the process in which your program goes through, from the code to the compilation process, to the JIT compilation and down to execution:

    C# Code --> .NET Assembly --> JIT Compilation --> Native Code --> Execution

    2.3 - Ngen Utility & "pre-JIT'ing":
    Ngen is a tool created by Microsoft corp. that acts as a native CLR compiler for .NET assemblies. It's availability has been there since .NET 1.1, although only recently has it dramatically improved functionality. As mentioned earlier, .NET assemblies get compiled to MSIL, not native code, and the JIT compiler compiles code in memory into native code that can be executed by the target machine.

    With Ngen, you can create a native image/cache for your assembly and essentially avoid having to go through the JIT compiler at runtime. Some people call this almost like pre-JIT'ing... And there are other ways too. This native image cache is an isolated store for all native assembly images on the local machine, in which the CLR loader will be able to locate for a specific assembly at runtime, if the native image exists. Otherwise the routine of regular JIT compilation will proceed.

    The benefit of this is faster startup time, code sharing between processes, and a smaller memory footprint of course. How much you'll notice these benefits is not guaranteed to be the same for every .NET assembly though.

    These native cached images can also be uninstalled just as easily as they are installed too.

    The downside is that not every assembly will be dramatically impacted by using Ngen, and there are several factors for this. If your .NET assembly is a strongly named assembly for instance, you may not notice the benefits, unless those assemblies are in the global assembly cache (GAC). The reason behind this is that the CLR loader does extensive validation on strongly named assemblies, that are not in the GAC, and this means that essentially every page in the native image will be looked at anyways... Thus by the time validation is complete, any startup speed gains that would have been noticeable have been delayed by this extensive scan and thus the benefits of using Ngen cancel out in that regard.

    Another way that most people do not know about is through the RuntimeHelpers.PrepareMethod method... Read here: http://msdn.microsoft.com/en-us/library/0bt50792.aspx

    2.4 - Dynamic Language Runtime:
    The dynamic keyword and Dynamic Language Runtime (DLR) came out with the official release of C# 4.0 for avoiding compile time checking. It's open source and is available on CodePlex: https://dlr.codeplex.com/

    A few examples of the dynamic keyword being used in C#, shows how variables can be assigned to different types on the fly, and also how compile time checks are not made, only runtime checking:

    This works:
    Code:
    dynamic x = 10; x = "value";

    This will compile but throw an exception at runtime:
    Code:
    dynamic x = "value"; x++;

    And to prove that the var keyword is not similar, this will not compile:
    Code:
    var x = "value"; x++;

• 3.1 - Basic Questions/Answers FAQ:
  1. When was C# created and by who? In the year 2000, the Microsoft Corporation founded the language of C#, initially to be part of the .NET family. The popularity of C# and .NET in general has risen vastly over the years though and is now almost becoming a standard.
  2. Why invent C#? Originally Sun wouldn't allow Microsoft to make changes to Java; Microsoft had a language called J++, but the changes they had made to this MS standard of Java had upset Sun, so they came out with other languages, including C#. Ever wonder why the syntax is nicely similar to Java?
  3. Why use C#? The C# language enables you to develop large scale applications quickly and efficiently. The .NET framework makes programming something of ease, and for what it may take to write the equivalent program in C++, would take perhaps days less, in C#. What makes C# most appealing for lots of people is that memory is managed for you through garbage collection, and exception handling is also implemented, through the .NET common language runtime, but it comes with lots of type safety, and GUI creation is also much less complex.
  4. What is the latest version of C#? By specification I believe the latest C# version as of now is C# 5.0.
  5. Object Oriented Programming... Yes or no? C# enables you to write programs with the OO statue in mind, yes.
  6. What is possible with C#? Game development, desktop applications, Windows Store apps, Web applications, Services, etc...
  7. C# vs. VB.net? I need to refrain from writing a bible on this, because there is a lot of opinion I have about this. Most obvious: C# is a much less implicit language (I have seen this used in a way to defend VB.net, which makes no sense at all!), unsafe code, the dynamic keyword, and syntax. (And no, Implicit variable declarations in VB.net is not like the dynamic keyword in C#, that is more similar to the var keyword in C#.)

• 4.1 - Features:
  • Outline:
    • Managed Code (1.0)
    • Generics (2.0)
    • Anonymous Methods (2.0)
    • Nullable Types (2.0)
    • Lambda Expressions (3.0)
    • Extension Methods (3.0)
    • Expression Tree (3.0)
    • Anonymous Types (3.0)
    • LINQ (3.0)
    • Implicit Types (var)
    • Late Binding (dynamic) (4.0)
    • Named Arguments (4.0)
    • Optional Params (4.0)
    • Better COM Support (4.0)
    • Async Feature (5.0)
    • Caller Information (5.0)

    C# has evolved as a language by far since the year 2000. The dynamic keyword implementation as of C# 4.0 allowed the language to build itself a connection with other dynamic languages, and allows variables to behave similarly to the way they work in other dynamic languages; Python, Ruby, etc... This also allowed a bridge between Python and C# specifically (IronPython), as well as Ruby (IronRuby). C# is still considered a static language however, because this is a static implementation of dynamic features.

• 5.1 - Specification:
  • The language of C# is an ECMA and ISO standard. (This is one of the factors that has influenced the creation of Mono, a Linux project.)

You can download the C# 5.0 Specification from here: http://www.microsoft.com/en-us/download/...px?id=7029

• 6.1 - Requirements:
  • - .NET Framework (installed by default if running Windows Vista or later). This can be installed as far back as Windows 2000 I believe... But different operating systems support different frameworks obviously because things inside the core of Windows change, and as a reminder, .NET is a framework. This means that it is a wrapper to all of the core functionality that Windows already provides inside other system dll's for the most part.
    - An IDE would be nice... Visual Studio, SharpDevelop, etc... I would HIGHLY recommend Visual Studio though. Depends on what you're doing of course, but this is about the language of C#, and not the IDE's so I'll leave this bit out.
    - *A little bit of motivation, problem solving skills, blah blah blah...


    Note: You can download the Visual Studio (Express or non-Express editions) or Visual C# Express as the IDE of choice. Express is obviously free, and the other is not, and is actually rather expensive. I have the full version, but if you're going with the express editions you should be fine. There are SOME setbacks to the express editions, so keep that in mind. You can easily look them up if you're curious.

• 7.1 - .NET Framework Information:
  • [.NET Framework 1.1]
    - Windows Server 2003

    [.NET Framework 2.0]
    - Windows Server 2003 R2

    [.NET Framework 3.0]
    - Windows Vista
    - Windows Server 2008

    [.NET Framework 3.5 SP1]
    - Windows 7
    - Windows Server 2008 R2

    [.NET Framework 4.0]
    - Windows 8

    [.NET Framework 4.5]
    - Windows 8
    - Windows Server 2012

    [.NET Compact Framework 1.0 (SP2)]
    - Windows Mobile 5.0

    [.NET Compact Framework 2.0]
    - Windows Mobile 6.0


    [Mono 1.1.13.6]
    - Ubuntu Dapper (6.06 LTS)

    [Mono 1.2.6]
    - Ubuntu Hardy (8.04 LTS)

    [Mono 2.4.2.3]
    - Ubuntu Karmic (9.10)

    [Mono 2.4 branch snapshot:
    - Ubuntu Lucid (10.04 LTS)

    [Mono 2.6.7]
    - Ubuntu Maverick (10.10)
    - Ubuntu Natty (11.04)

• 8.1 - Sample Code:
  1. Hello World: The infamous hello world application, written in C#.
    Code:
    namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.ReadKey(); } } }

  2. LINQ Sample: This code shows an example where s1 is queried into a modified version which is assigned to s2 and outputted to the console. With the LINQ, what happens is every individual char from s1 is "looked at" and if the index of that char in the string is even, we take the int value of that ASCII character and increment it by 1, otherwise we decrement it by 1 and assign this IEnumerable result to a new string, which yields "64646."
    Code:
    string s1= "55555"; string s2 = new string(s1.Select((c,i) => i % 2 == 0 ? ++c : --c).ToArray()); Console.WriteLine(s2); // "64646"

  3. My Rx Sample: This snippet uses the Rx Reactive extension framework, and creates an asynchronous callback to update the Text property with the time after being subscribed to the interval from the TimeSpan object.
    Code:
    IObservable<long> timeObservable = Observable.Interval(TimeSpan.FromSeconds(1)); timeObservable.Subscribe(i => Invoke((MethodInvoker)delegate { Text = string.Format("Time -> {0}", TimeSpan.FromSeconds(i)); }));

I'll be here to extend this thread a bit throughout time perhaps. I'm not good at writing s**t like this to be honest. Smile
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply





Messages In This Thread
The C# Programming Language Introduction - by ArkPhaze - 07-02-2013, 04:53 AM



Users browsing this thread: 1 Guest(s)