String Theory - DotNet7

String Theory - DotNet7

In dotnet 7 strings are getting some love with a few improvements that will make devs life a little easier. Dotnet 7 introduces a new attribute for strings which gives your editor the context in which the string is to be used, this means that it can assist developers with code completion and warnings about strings that don't meet the required syntax.

To give this a concrete example you can set a string as being json with the StringSyntax attribute taking StringSyntaxAttribute.Json as the argument in the following code:

	function Setup()
	{
		string jsonValues = @"{
			""value1"":"3"
			""value2"":4
		}";

		var intValues = ExtractValuesFrom(jsonValues);
	}

	private int[] ExtractValuesFrom([StringSyntax(StringSyntaxAttribute.Json)] string jsonblob)
	{
		var intValues = new int[]{};
		//Process json and return intValues
		return intValues;
	}
        

The above code has errors in the json that without the support of the new attribute would not be triggered until run time since jsonValues is a valid string but not valid json. The new features will highlight the fact that the json is not valid and missing a "," , the line should read ""value1"":"3",

The StringSyntax attribute will not give runtime errors relating to strings, if used on an api endpoint it won't validate string values being passed in, a string is still a string and will be treated as such at runtime. It is to be used as an aid during coding for the following reason:

  • Show the intent of what the string represents
  • Give warning messages of strings being passed in and whether they meet the syntax formatting requirements of specified string type
  • Give better code completion on the strings, etc if set as a regex the ide should show guidance on writing a regex.

As of now (29th September 2022), using?this on Visual Studio Mac these features have not been integrated but should be coming soon.

DotNet 7 is in preview, this article the code was tested again Dotnet 7.0 RC1 running on a Mac with Visual Studio Mac 17.4 Preview

thanks to?Steve Johnson?for the string photo, found on?Unsplash

要查看或添加评论,请登录

?? Andy S.的更多文章

  • Dotnet Containers - now and when

    Dotnet Containers - now and when

    .Net has evolved over the years from being exclusively for windows then through the work of the mono project its become…

  • Minimal Minimum API

    Minimal Minimum API

    In this article we will create an api using the new .net-6 feature called the "Minimum API" and use an absolute minimum…

  • 1000Km of commuting by bike

    1000Km of commuting by bike

    After several months of cycling on a regular basis I have hit racked up 1000 kilometres on my bike. This is a good time…

    4 条评论
  • My health and fitness

    My health and fitness

    I've decided to take better care of myself and so am increasing the aerobic and strength fitness I do. So this week I…

  • Cycling to work - the kit list

    Cycling to work - the kit list

    I have started regularly commuting to work on bike, it's not much longer time wise than driving, gives me some exercise…

  • The 1 Minute?Pitch

    The 1 Minute?Pitch

    It’s Friday, the start of the Startup Weekend, you’ve chatted, ate tasty pizza, heard tales of toil and triumph from a…

  • Startup weekend kit list

    Startup weekend kit list

    These are only suggestions, but here are a few recommendations to get the most out of a Startup Weekend. Water bottle:…

  • Smoke, Startups and Mirrors

    Smoke, Startups and Mirrors

    Startup Weekends normally involve several projects with a technical based idea that ultimately wants a website or app…

    1 条评论
  • Get involved in Derbys first Startup Weekend

    Get involved in Derbys first Startup Weekend

    Derby is hosting its first ever Startup Weekend on March the 10th to the 12th. Derby with its huge pool of talented…

  • My First Startup Weekend

    My First Startup Weekend

    My first Startup Weekend was in Nottingham, put on by Nottinghams amazing Creative Quarter team, and I loved it. It was…

社区洞察

其他会员也浏览了