Sadie  Ratke

Sadie Ratke

1593514638

Introducing C# 9: Records

Introduction

C# 9 introduces Init-only properties that allow to make individual properties immutable. C# 9 introduces another great feature that enable a whole object to be immutable and make it acting like a value: Records. Let’s see in this article how Records work. Unlike the previous announcement from Microsoft (https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/), data class** (combined) keywords become now record keyword.

Data keyword

C# 9 Introduces a new keyword: **record **keyword. **record **keyword makes an object immutable and behave like a value type. data keyword replaces the init keyword for each property if you want the whole object (all properties) to be immutable.

Example:

namespace CSharp9Demo.Models
	{
	    public record Product
	    {
	        public string Name { get; init; }
	        public int CategoryId { get; init; }
	    }
	}

The important thing to know here with Records is that members are implicitly public if you don’t precise it. Then the following class declaration is similar to the previous above:

namespace CSharp9Demo.Models
	{
	    public record Product
	    {
	        string Name { get; init; }
	        int CategoryId { get; init; }
	    }
	}

Records introduce also public init-only auto-property (if you don’t to use explicitly private fields) which is a shorthand of the previous declaration (same meaning):

namespace CSharp9Demo.Models
	{
	    public record Product
	    {
	        string Name;
	        int CategoryId;
	    }
	}

With-expressions

We might want sometimes create new a object from another one because some property values are identical only one change, unfortunately your object is immutable. **with **keyword fixes that. It allows you create an object from another by specifying what property changes:

using System;
	using CSharp9Demo.Models

	namespace CSharp9Demo
	{
	    class Program
	    {
	        static void Main(string[] args)
	        {
	            var product = new Product
	            {
	                Name = "VideoGame",
	                CategoryId = 1
	            };

	            var newProduct = product with { CategoryId = 2 }

	            // newProduct.Name == "VideoGame"
	            // newProduct.CategoryId == 2
	        }
	    }
	}

#.net 5 #csharp #csharp 9 #programming-c

What is GEEK

Buddha Community

Introducing C# 9: Records

Let's Give Some Unit Testing Love to C# 8 and C# 9 Features

According to StackOverflow, C## is one of the most-loved programming languages. And I completely understand that—it is powerful, easy to learn and consistently improving and developing. It is a living language. :)

The last couple of years, there were new features added to the languages, and the new versions keep coming up—C## 7, C## 8, C## 9.

As you know, we at Progress Telerik are proud that our products are always in sync with the latest things in the .NET world, and C## 9 and JustMock are no exception.

#c #c# #c#8 #c#9

Tamale  Moses

Tamale Moses

1624240146

How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++

Dicey Issues in C/C++

If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends. The following displays some of the issues:

  1. Using multiple variables in the print function
  2. Comparing Signed integer with unsigned integer
  3. Putting a semicolon at the end of the loop statement
  4. C preprocessor doesn’t need a semicolon
  5. Size of the string matters
  6. Macros and equations aren’t good friends
  7. Never compare Floating data type with double data type
  8. Arrays have a boundary
  9. Character constants are different from string literals
  10. Difference between single(=) and double(==) equal signs.

The below code generates no error since a print function can take any number of inputs but creates a mismatch with the variables. The print function is used to display characters, strings, integers, float, octal, and hexadecimal values onto the output screen. The format specifier is used to display the value of a variable.

  1. %d indicates Integer Format Specifier
  2. %f indicates Float Format Specifier
  3. %c indicates Character Format Specifier
  4. %s indicates String Format Specifier
  5. %u indicates Unsigned Integer Format Specifier
  6. %ld indicates Long Int Format Specifier

Image for post


A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a non-negative integer in the range [0 to 4294967295]. The signed integer is represented in twos-complement notation. In the below code the signed integer will be converted to the maximum unsigned integer then compared with the unsigned integer.

Image for post

#problems-with-c #dicey-issues-in-c #c-programming #c++ #c #cplusplus

Loma  Baumbach

Loma Baumbach

1603381251

Introducing C# 9: Static anonymous functions

Introduction

C## 9 brings an important improvement to anonymous functions by allowing the modifier static on them and we now have static anonymous functions ! Why Microsoft brought this feature ? Because allocation matters ! Microsoft explains here that lambda are not cost less (https://devblogs.microsoft.com/premier-developer/dissecting-the-local-functions-in-c-7/) :

“Anonymous methods are not cheap:”

  • Overhead of a delegate invocation (very very small, but it does exist).
  • 2 heap allocations_ if a lambda captures local variable or argument of enclosing method (one for closure instance and another one for a delegate itself)._
  • 1 heap allocation_ if a lambda captures an enclosing instance state (just a delegate allocation)._
  • 0 heap allocations_ only if a lambda does not capture anything or captures a static state._

#c# #.net 5 #c# 9 #static anonymous function #c++

Sadie  Ratke

Sadie Ratke

1597050373

Introducing C# 9: Extending Partial Methods

Introduction

C## 8 (and above) has some restrictions regarding partial methods. For example :

  • Partial methods must have a void return type
  • Partial methods can’t have out parameters
  • Partial methods can’t have any accessibility keyword (public, private, protected etc….)

C## 9 aims to remove these restrictions. If you want to learn more about the motivation behind this, you can find a good description on the Github page here: https://github.com/jaredpar/csharplang/blob/partial/proposals/extending-partial-methods.md

Behavior before C## 9

Below are some examples of what happens when a partial methods has or not an accessibility keyword, has or not an out parameter, a void or not return type, implements an interface:

partial class MyService : IMyService
	{
	      partial void MyFirstFunction(); // Ok
	      private partial void MySecondFunction(); // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers
	      private partial void MyThirdFunction(); // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers
	      private partial object MyFourthFunction(); // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers + CS0766 Partial methods must have a void return type
	      private partial object MyFifthFunction(); // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers + CS0766 Partial methods must have a void return type
	      private partial void MySixthFunction(out int result); // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers + CS0752 A partial method cannot have out parameters
	      public partial void MySeventhFunction(); // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers
	}

	partial class MyService
	{
	      private partial void MyThirdFunction() { } // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers
	      private partial object MyFifthFunction() { return new { }; } // CS0750 A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers + CS0766 Partial methods must have a void return type
	}

	public interface IMyService
	{
	      void MySeventhFunction();
	}

#c# #.net 5 #c#9 #extending partial methods #programming-c #csharp