---
title: "Script# (Script Sharp) – writing javascript in C#"
url: "https://osherove.com/blog/2009/8/11/script-script-sharp-writing-javascript-in-c.md"
canonical: "https://osherove.com/blog/2009/8/11/script-script-sharp-writing-javascript-in-c.html"
published: "2009-08-11T15:18:04"
updated: "2009-08-11T15:18:04"
tags: [".NET", "C#", "web"]
author: "Roy Osherove"
description: "I’ve started dabbling with web development. I’m building a secret project that I’ll unleash one day. soon. I’m learning a lot of JS and JQUery foo as I’m putting together a coupld of ajax and interaction intensive pages…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Script\# (Script Sharp) – writing javascript in C\#

By Roy Osherove August 11, 2009 In [.NET](https://osherove.com/blog/category/net), [C#](https://osherove.com/blog/category/c), [web](https://osherove.com/blog/category/web)

**Historical article** · Originally published August 11, 2009; technical details may be outdated.

I’ve started dabbling with web development. I’m building a secret project that I’ll unleash one day. soon.

I’m learning a lot of JS and JQUery foo as I’m putting together a coupld of ajax and interaction intensive pages on the site.

The javascript experience has been not so great. While it’s a piece of cake to get small scripts going, once you get beyond a couple of pages long script files, you get into a nightmarish scenario where navigating, refactoring or sometimes understanding the relationships in your javascript code becomes slow to nearly impossible.

there’s a lot to be said for the open way in which javascript works. but it’s too “open” in terms of programming experience.

So I looked at way to arrange javascript better. from ways to arrange files and classes, to loading JS dynamically. it all pretty much sucks.

I can’t believe this is the state of the art after a few good years of web 2.0 programming. really.

Enter Script#.

This thing has potential to be huge.

It’s a custom compiler that sits inside visual studio and gives you “special” projects that you write like C# libraries. the code that you write then gets compiled to both a dll (which you don’t care about) **and** to a javascrip file (in two versions – a minified version and a debuggable version).

it’s basically a translation of the C# code you write into javascript.

this allows me to write a whole bunch of very interactive scripts, but they are easily managed like regular C# files and projects.

- I can refactor the code easily
- I can navigate easily
- there’s a representation of the document and script object models has C# frameworks, so I get intellisense
- I can also use JQuery and other things which I depend on (like jquery plugins etc..)

the only thing you don’t get is the debuggability. for that you still need to debug javascript code, which the generated code is very readable.

- It does not support generics yet, which is OK. You don’t need generics to do great stuff in javascript code. but it would be nice to have!
- It does not support JQuery out of the box. there is a solution, and the end of this post.
- It still has some quirks and things you need to learn using the script objectmodel so that you get what you intend in your javascript code.

overall, despise the very different nature of C# and JS, I find that with Script# I almost manage to have it all: Great javascript interaction, but a “managed” experience when writing that code.

**How can I get it?**

- [you can get Script# here](http://projects.nikhilk.net/ScriptSharp/)
- [here’s a good doc on intro to Script# plus lots of "common scenarios” in JS](http://www.scribd.com/doc/13743466/ScriptSharpv042) and how you implement them using Script#.  For example, to implement sending in a JSON object with various properties, you use a special “Dictionary” class with values and keys.
- you can get JQuery support by using [the “jQuerySharp” project](http://code.google.com/p/jquerysharp/) .

**Including JQuery into the mix**

- I downloaded and extended the [JQuery for Script# project](http://code.google.com/p/jquerysharp/). it’s very easy to “get” and get going once you grokked Script#.
- The “$” sign is out. You can’t use the “$” sign since it’s not allowed in C#. so the generated code actually uses it in other places in the generated code (it was not built with jQuery in mind). So if you have other non generated JS code that uses jQUery you’ll need to use the “safe” version of jQuery (“$(‘something’)” becomes “jQuery(‘something’)” )
- you will need to add any plugins jQuery uses on top of that. it’s very simple:

The thing you need to realize about “supporting” other frameworks, is that when you code in C# you need to think at a different level - “what will be generated from this?”. There are two kinds of code in script# –

- there’s “regular” code which you write which gets all translated into javascript.
- and then there’s “Exported” code, which is just a strongly typed “shell” you can program against, but does not really get generated on the JS side. You note that code by putting a couple of attributes on the class.

for example, the JQuery additions to Script# are just one class called jQUery with methods that have the same signatures as methods in the jQuery object model, but with no implementation but the simplest one to make them compile. **any calls to this class will be generated**, but the class being called will not be generated. You are in fact saying to the compiler ‘expect that there’s already an existing object model I will work against" – ignore this class when you generate code”. very cool stuff.

**Things I’d love to see in Script#:**

- More frequent updates and .net 3.5 support.
- Features: I’d love to be able to use “new {Prop=”value”}” anonymous types to generated JSON objects. also I’d love to have lambda support instead of just a simple delegate support.
- Open source it baby. so we can all contribute and learn from it.

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- Aug 11, 2009

    ### [Uncle bob and the Art of Unit Testing](https://osherove.com/blog/2009/8/11/uncle-bob-and-the-art-of-unit-testing.html)

- Aug 11, 2009

    ### [No Excuses](https://osherove.com/blog/2009/8/11/no-excuses.html)

- Jun 30, 2009

    ### [Lifetime of a software methodology](https://osherove.com/blog/2009/6/30/lifetime-of-a-software-methodology.html)

- Jun 28, 2009

    ### [JQuery in Action – Great book](https://osherove.com/blog/2009/6/28/jquery-in-action-great-book.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
