Keyword Implementation in C# - c#

Is there a way, and how would I go about implementing my own keyword such as in, and as (etc), to be used in my code?
Here is what I had in mind. I want to (just for my own personal reasons, I guess) add a few keywords of my own, one of which would be the "was" keyword:
if(Control was Clicked)
{
// etc etc
}

No.
The closest you could get would be an extension method:
Control.WasClicked()

No, you can't add to the C# language, short of writing your own compiler.
However, your "was" keyword makes me think you might be looking for a way to declaratively handle events. Microsoft have a library called "Reactive Extensions for .NET" (Rx) that is an extension to LINQ that allows you to deal with events in a declarative fashion.

No, you cannot add keywords to C# - at least not without writing a compiler for yourself. If what you want to do is simple, however, perhaps you could do it using a custom preprocessor. You would lose some syntax highlighting and error checking in Visual Studio, though.
One language for the CLR, that is designed to be extensible like that, is Boo.

Related

Is it possible to add keyword to C# or VB.NET?

I know it might not be worth it but just for education purposes I want to know if there is a way to inject your own keywords to .NET languages.
For example I thought it's good to have C++ asm keyword in C#.
Remember I'm not talking about how to implement asm keyword but a general way to add keyword to C#.
My imagined code :
asm{
mov ax,1
add ax,4
}
So is there a way to achieve this ?
The answers which cover implementing keyword{ } suits enough for this question.
This isn't possible at the moment. However, there's a Microsoft project in development called Roslyn that can be summarised as "the compiler as a service." It allows you, amongst other things, to extend or modify the behaviour of the compiler through an API.
When Roslyn becomes available, I believe this should be something that (with caution!) is quite doable.
You can use whatever tools you would like to pre-process your code before sending it to the C# compiler. For example, you might use VS macros to do the pre-processing, mapping a given syntax that you invented into something that does compile into C# code, possibly generating an error if there is a problem. If VS macros aren't powerful enough for you then you can always use your own IDE that does whatever you code it to do to the text before sending it to the compiler.
There is no built in support in the compiler for specifying your own keywords/syntax; you would need to handle it entirely independent of the compiler.
Unfortunately this is not possible. You can't extend or alter the languages in any way.
You could in some obscure way use PostSharp to read and parse strings and transform them to custom code at compile time (a pre processor). But you would not get very happy with that, as it is very error prone and you won't get any kind of intellisense or code completion for your magic strings.
According to MSDN keywords are predefined and cannot be altered. So you can't add any, because you would need to tell the compiler how to handle them. Insofar, no you can't.

c# - make all vars explicit typed at once

The var keyword in c# is a great time saver while your coding, but when you are looking back at code that's not fresh in your mind, it's more helpful to have explicit types in the code.
I have code rush express which allows me to "Make Explicit" 1 var keyword at a time.
Are there any tools or add ins that will convert many of them at once?
Yes Resharper can do this using the code clean up macros. Whether you want to enforce explicit or implicit types is a setting.
Coderush is based on an open framework or api freely available on DX website. Not sure now but i think is called DxCore. you can download it and play with that to make your own extension for CodeRush. I would not do it anyway, there is really nothing wrong in the usage of var unless some very specific cases when it is really not obvious to understand what it will contain.

Is it possible to create C# language modifications as did LINQ?

I've been looking quite a bit at Mr. Skeet's blog on how to re-implement LINQ.
In particular, he states that the code:
var list = (from person in people
where person.FirstName.StartsWith("J")
orderby person.Age
select person.LastName)
.ToList();
is translated to methods that are extension methods that are provided by the LINQ library:
people.Where(person => person.FirstName.StartsWith("J"))
.OrderBy(person => person.Age)
.Select(person => person.LastName)
BY THE COMPILER.
My question is, how does one impress the bigwigs enough with a library to cause them to allow the language to change to support the library? Or were those words already reserved before LINQ came along?
Grab the Mono C# Compiler - it's open source and you can do whatever language modifications you want and which .net supports, e.g., use enums as generic constraints, create methods that return references to value types (public ref int Max(ref int x, ref int y) { if (x>y) return ref x; else return ref y; }), that have protected or internal visibility etc.
Of course, you are then creating an incompatible derivate of C#, but if you push it hard enough then people might like it.
The other option: Start a blog, come up with some really good use cases for it, possibly a sample implementation in a .net language or using a customized compiler, show what problem it solves and why this would be a big win that justifies the cost that goes into specifying, designing, developing, testing and documenting of the feature.
I highly doubt the compiler developers would implement syntax extensions for just any library. If you really wanted language-level integration, you could develop a pre-processor that transforms your custom syntax into valid C#. That's essentially what the compiler does with LINQ anyway (as you pointed out in your question).
Of course, you would lose things like auto-complete and syntax highlighting in Visual Studio, but that could be fixed with an extension.
Some languages allow to extend their syntax and semantics. The closest to C# is Nemerle (and it even supports a safe subset of C#, which you can, in turn, extend as you like), but the same can be done with almost any Lisp, for example. So, if you're using a language powerful enough, you don't need to "impress" anyone - any library can add new functionality to a language itself.
There were rumors that the next C# will provide some rudimentary metaprogramming support as well, but I could not find any specifics.
It is possible but it will be hard and they'll probably reimplement the idea to better fit their language constructs. The new async feature is similar to a library called AsyncEnumerator for example but they are building everything to better suit the language. The keywords for LINQ were not reserved in advance but they are contextual keywords meaning that there can be identifiers that match this keywords out of the LINQ context. When the compiler detects a LINQ construct it goes into LINQ mode where these keywords are actual reserved words.

code snippets interpretation

Let's say I have a WinForm App...written in C#.
Is it possible?
After all, put my eye on Iron Python.
C# is not interpreted, so unlike javascript or other interpreted languages you can't do that natively. You can go four basic routes, listed here in order of least to most complex...
1) Provide a fixed set of operations that the user can apply. Parse the user's input, or provide checkboxes or other UI elements to indicate that a given operation should be applied.
2) Provide a plugin-based or otherwise dynamically defined set of operations. Like #1, this has the advantage of not needing special permissions like full trust. MEF might come in handy for this approach: http://mef.codeplex.com/
3) Use a dynamic c# compilation framework like paxScript: http://eco148-88394.innterhost.net/paxscriptnet/. This would, in theory, allow you to compile small c# snippets on demand.
4) Use IL Emit statements to parse code and generate your operations on the fly. This is by far the most complex solution, likely requires full trust, and is extremely error prone. I don't recommend it unless you have some very obscure requirements and sophisticated users.
The CSharpCodeProvider class will do what you want. For a (VERY outdated, but still working with a few tweaks) example of its use, check out CSI.
If you are willing to consider targeting the Mono runtime, the type Mono.CSharp.Evaluator provides an API for evaluating C# expressions and statements at runtime.

Can C# allow to override the this keyword like in Javascript to make Jquery-like library possible?

From
http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
When defining event handlers in
jQuery, the library will take care of
overriding the value of this and make
sure it contains a reference to the
element that was the source of the
event.
How does jQuery override the value of
this? Keep reading.
apply() and call()
Can C#/.NET give that same power ? If not how can one build a jquery-like library for C# not for javascript only ?
No, there is no way to change this in C# but that is not required for building fluent interfaces with concepts like method chaining (see the method chaining wiki page for an example in C#).
No, C# does not allow you to redefine language elements.
C# is a statically typed language whereas javascript is a dynamic language - just one difference that means you can't override language elements in C# but can in javascript.
Regardless, I don't quite see how you would write one library that would work for both javascript and C#.
No, you can't. this is a reserved keyword that is set automatically, and you really want it to point to the right place, ie, the current instance of the class.
Besides, what would exactly a 'jquery-like library for C#' be like? I don't think the concept makes much sense.

Categories