Muhammed Kılıç
Posted on August 30, 2021
Hello Dev Community,
Until 2 years ago I have never used C#, I was mostly using C and Java but after starting my job with .NET stack, I started to learn C#. I easily learned C# with the help of similarities in syntax with Java.
Anyway I was searching about C#'s .NET compiler called Roslyn and I have come across with Roslyn team's work in progress documentation. I wanted to read, understand and tell you people with summarizing it. I want to also note that I don't know either this features will be published in the next version or the next one. Indeed there are people working on these features.
1- Support for method parameter names in nameof()
Author of proposal suggested a usage for nameof method for converting this usage:
[TestFilter(FilterInput = "testParam")]
public string TestMethod(string testParam){return testParam;};
to this:
[TestFilter(FilterInput = nameof(TestMethod.testParam))]
public string TestMethod(string testParam){return testParam;};
After a few comments another user asked about usage when method overload happens and bringing nameof method parameters into the method scope. The author has suggested a usage like below finally and this feature is under development as I see from issues on GitHub repository.
// Methods
TestMethod(string a);
TestMethod(string a, string b);
// Usage of nameof
nameof(TestMethod.a, string) // "a"
nameof(TestMethod.a, string, string) // "a"
nameof(TestMethod.b, string, string) // "b"
I liked this feature as another user also suggested if we can pass nameof
parameters into method scope, writing tests will be much easier for some cases.
2- Simplified parameter null validation code
I think my favorite feature in this list is parameter null-checking. For many times I have written methods like:
string TestMethod(string testParam){
if(testParam == null) {
throw new ArgumentNullException(nameof(testParam));
}
// Some logical code maybe?
return testParam;
}
Can you guess about passing several objects as parameters to methods. Doing null-checking was a trouble for me. That's why I liked this feature most. If this becomes a feature of C# we will start to use basically like this:
string TestMethod(string a!, string b!){
// Again some logical code.
return a + b;
}
Probably using !
will not be final version for this feature. They already have alternatives for syntax of null-checking:
T p!
T p!!
T! p
checked T p
nonnull T p
For !!
syntax option null-checking may be used for expressions as I read from Language Design Meeting notes.
3- Raw string literal
This may look similar to you from Python, Javascript or Java.
In Python triple quotes which are """
or '''
used for multiline strings, newlines and other special characters. Using r'some raw string'
in Python is another alternative. You don't need to escape from any character. Discussions on raw string feature is very harsh to me. If you are reading this please be kind.
This feature is still at proposal level so there are questions which need answers. Are starting and ending delimiter will be """
or """"
?
How interpreter will result with indentations, whitespaces and EOF newlines? How will we pass any variable into this raw string, and what will be the interpolation syntax?
There are many undecided questions but this syntax would be useful for HTML or XML files for example:
var htmlElement = """
<a href="https://dev.to/">DEV</a>
""";
4- Allow Generic Attributes
This feature is referenced to type converter example in proposal. Basically you will change and use your attributes from [TypeConverter(typeof(X))]
to [TypeConverter<T>]
. You will directly have type constraints with this type-referencing attribute.
public class MyGenericAttribute<T> : Attribute { }
[MyGenericAttribute<T>]
public class MyGenericClass<T> {
[MyGenericAttribute<T>]
public void MyGenericMethod(T t){
// ...
}
}
Have you ever wanted to use generic attributes in your code? If your answer is yes, then here you go! To mention, this feature is in progress. Only thing I miss is this feature have been developing since 2017.
There are some other features in Language Feature Status list also. You can check and write your opinions in the comments to keep tidy or I can add into this post.
I hope you enjoyed my self speech while reading language features. :)
Take care, folks!
Posted on August 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.