Exploring color-contrast() for the first time.

Exploring color-contrast() for the first time.

I saw in the release notes for Safari Technical Preview 122 that it has support for a color-contrast() function in CSS. Safari is first out of the gate here. As far as I know, no other browser supports this yet and I have no idea when stable Safari will ship it, or if any other browser ever will. But! It’s a very good idea! Any native tool to get us to ship more accessible interfaces (of which color contrast is a part) is cool by me. So let’s attempt to get it to work.

Anybody can download Safari Technical Preview, so I did that.

I had to ask around about this, but just because this is a pre-release browser, it doesn’t mean all these features are active by default. Just as Chrome Canary has feature flags you have to turn on, so does Safari Technical Preview. So, I had to flip it on like this:

The release notes don’t have any information about how to actually use color-contrast(), but fortunately web searching turns up a spec (it’s part of Color Module 5), and MDN has a page for it with very basic syntax information.

This is how I understand it:

That example above is a little silly, because it will always return white — that has the most contrast with black. This function actually gets useful when one or more of those color values is dynamic, meaning very likely it is a CSS custom property.

The function returns a color, so the top use-case, I would guess, is going to be setting a color based on a dynamic background. So…

section {
  background: var(--bg);
  color: color-contrast(var(--bg), white, black);

Now we can toss any color at all at --bg and we’ll either get white or black text, depending on what has the most contrast:

That’s extremely cool, even in the most basic use case.

Here’s a demo from Dave where he’s not just setting the text color in the parent, but the color of links as well, and the links have a different set of colors to choose from. Play with the HSL sliders (in Safari Technology Preview, of course) to see it work.

Just picking two colors that have enough contrast is easy enough (although you’d be surprised how often it’s screwed up by even those of us with good intentions). But oh wow does it get complicated quick with different situations, let alone having a bunch of color variations, or god forbid, arbitrary combinations.

Here’s a video of me playing with Dave’s tester so you can see how the colors update at different places.

Source link


I developed a website to check new film releases and check i…

After starting to work with Angular by my own a few months ago I just developed my first project:


This little Angular web-app it’s hosted on firebase and let you check new movie releases and based on your location finds out on what platforms can you see a movie.

I haven’t finished yet but I ‘d love to have feedback about functionality or design. What do you think?

r/webdev - I developed a website to check new film releases and check in what platforms can you find a movie based on your current location.

Atlantida Angular Web App

Source link

Remote Pair Programming with Visual Studio Code and Live Sha...

Remote Pair Programming with Visual Studio Code and Live Sha…

Remote work has killed pair programming. Or has it? If anything, it certainly presents an interesting challenge for pair programming when the two developers involved are no longer sitting side by side in the same room. But can you still successfully pair program in this world of remote work?

In this article, we’ll look at some of the benefits of pair programming and how you can use technology like the Visual Studio Code Live Share extension to continue to pair program remotely with your colleagues.


What is Pair Programming?

Let’s start with a quick definition. We’ll define pair programming as a development technique in which two developers write code at a single workstation. Typically this means only one computer and one keyboard. The developer at the keyboard “drives” while the developer without the keyboard “observes” or “navigates”.

The driver provides the hands that write the code. The observer-navigator provides the strategic decision-making for which files to look at and which problems to tackle. The observer-navigator also reviews the code that is being written and helps catch simple typos and other mistakes in real time. The two developers switch roles at some agreed-upon interval, typically every 30 minutes or so.


Benefits of Pair Programming

Now, why on earth would anyone do this? Two developers working on the same thing at the same time? Surely that’s a waste of time. Couldn’t you produce twice as much code if the developers were working independently on their own machines?

These are certainly valid arguments and ones worth examining. So let’s look at the benefits of pair programming. What good do we get from two developers working together?

Teaching and Mentoring Opportunities

First off, pair programming provides a perfect teaching opportunity. It’s rare that the two developers are at exactly the same skill level. Instead, one is usually more senior and the other more junior. Pair programming allows the senior engineer to mentor the junior engineer as they work on real problems together. The junior engineer can learn how the senior engineer reasons through a problem, how they organize their code, how they name their variables, and how they troubleshoot issues when things go wrong. It’s also important to remember that the mentoring and teaching is a two-way street.

And seniors, don’t underestimate your junior engineers. They may have a fresh perspective on the problems at hand too. And who knows, you both may end up learning each other’s favorite IDE shortcuts and tricks.

Knowledge Sharing

Second, pair programming allows you to share knowledge regarding how the system is architected. When developers work in individual silos, you’ll often have one engineer code a feature from start to finish. Even though code reviews are done throughout the development process, having a single engineer be the main point of contact on a project results in that one person having the most understanding and knowledge of the final solution. Pair programming, on the other hand, results in two developers both gaining a deeper understanding of their code.

Error Prevention

Third, pair programming makes it easy to catch simple mistakes and typos faster with a second pair of eyes. You’ll be surprised how much faster the two of you can find the missing semicolon or why your code won’t compile when you have a second person watching every character you type.

Interpersonal Communication Skill Improvement

Fourth, pair programming is a great opportunity for engineers to develop their interpersonal skills. Can you talk through a solution with another person? Can you bounce ideas off each other? Can you clearly communicate your ideas in a way that another engineer can understand? Can you tactfully guide someone away from a poor solution if you see a better one? And perhaps most importantly, how do you respond to feedback and correction on your own ideas? These soft skills are valuable for engineers at any level.

Defect Reduction and Increased Productivity

Finally, and perhaps most interestingly, studies have shown that when engineers pair program they write better code with fewer bugs. The extra development-time cost of having two people work together is estimated at 15%. But when you think about all the time saved through fewer customer support issues and less time spent debugging, you may find that pair programming actually increases productivity!


Pair Programming Remotely

So, we’ve established that pair programming has several benefits. But how do we pair program remotely? COVID-19 has forced many companies to adopt a remote work policy, even those companies that may have initially been very averse to the idea of remote work.

As for me, I’ve typically worked at the office, but I’ve been working remotely since March 2020, and I’m loving it! Zoom meetings and Slack messages have become my default methods of communication. But what about for pair programming?

One option might look something like this:

  1. Dev 1 and Dev 2 get on a video call together
  2. Dev 1 starts as the driver, and Dev 2 starts as the observer-navigator
  3. Dev 1 shares their screen and starts writing code while Dev 2 navigates
  4. When it’s time to switch roles, Dev 1 commits what they’ve written and pushes it to a branch on the remote repository
  5. Dev 2 pulls down the latest code from the branch
  6. Dev 2 shares their screen and starts writing code while Dev 1 navigates
  7. When it’s time to switch roles again, Dev 2 commits what they’ve written and pushes it to the branch
  8. Repeat steps 3-7 until the pair programming session is over

Those steps look pretty similar to what you might see when pair programming in-person, with the exception of the video call and screen sharing. The other notable difference is the need to commit the code and push it up in order for the other person to have the latest code. When pair programming in person, you don’t have that same challenge, as you’re both working on the same computer.

So that begs the question: Is there any way to collaborate without having to commit your code in between switching roles?

The answer: Yes, there is! Visual Studio Code (VS Code) has a handy extension called Live Share that helps facilitate the remote pair programming process. Live Share allows you to share your code while others follow along in VS Code on their own computer. But that’s not all – you can even share your localhost server or a terminal instance!


How to Use VS Code Live Share

You can find the VS Code Live Share extension for free on the Visual Studio marketplace. After clicking the Install button and then restarting your VS Code application, you’re ready to start collaborating! They have excellent installation instructions and a quick start guide on their marketplace listing at the same link as above, so I’d highly recommend following along there.

To experiment with this extension, I created a simple Node.js project hosted on Heroku. I opened the project directory in VS Code on my own machine, clicked the Live Share icon on the left-hand side of the screen, and then clicked Share to create a new collaboration session.

Click the Share button to start a new collaboration session

Since this was my first time using the extension, I was prompted to sign in, which I did through GitHub.

Sign in with your GitHub or Microsoft account

After signing in, I was given a collaboration session URL which I could then share with up to 30 people at a time.

Send the collaboration session URL to anyone you’d like

I sent this URL to myself on a second computer and signed in as an anonymous John Doe user. After doing that, I received a notification on my first machine that John Doe was attempting to join my collaboration session. At that point, I could grant read-only access to let him follow along, read-write access to let him write code along with me, or reject his request and keep him out of the session. I clicked the “Accept read-write” button.

Accept or reject requests to join your session

Over on my second computer, once access was granted, VS Code pulled open the project directory on my machine. This is really neat! I didn’t even need to pull down the code locally or clone the GitHub repo.

Once multiple people are in the session, each person’s cursor is shown, and you can collaborate in real time, much like editing a Google doc together. As you can see below, I was able to write a line of code from both computers, and I can see two cursors on the screen.

Multiple cursors can be seen in VS Code, one for each participant

I was running the app locally on my first machine by using the npm start command. The app output running on localhost:5000 is seen below:

Demo app running locally on localhost:5000

Now, here’s where it gets really exciting. Beyond just writing code together, I can also share my localhost server with others in the session. I chose to share localhost:5000. Then, on my second computer, I navigated to localhost:5000, and I could see the app there too! As a collaborator, not only did I not need to clone the repo, I also didn’t need to install any dependencies or run the app locally on my own machine. All of that was handled by the first user!

Share a session, servers, or terminals with others

In addition to sharing servers, you can also share terminals with collaborators. I shared a terminal from my first computer, and then on my second computer I was able to enter the npm test command in an attempt to run the project’s test suite. (But alas – there are no tests to run.)

Shared terminal instance

During the collaboration session I added a new section to the app titled “Features to Try.” Our Live Share collaboration session was a great success!

End result for the demo app



I was honestly really impressed with how smoothly things went using Live Share and all the additional features it offered beyond simple code-editing collaboration. The only real challenges presented would be if VS Code was not your preferred IDE or if anyone in the pair programming session experiences internet connectivity issues.

Pair programming yields many benefits: fewer defects, higher productivity, and excellent mentoring opportunities. COVID-19 may have forced us all to work remotely, but that doesn’t mean we can’t still pair program. With tools like VS Code Live Share, remote pair programming can be just as easy as pair programming in the office.

What has your remote pair programming experience been like? I’d love to hear from you in the comments.

Source link

r/graphic_design - How to do this line texture style?

How to do this line texture style? : graphic_design

Hi, I’m trying to learn Photoshop. I am fairly familiar with Illustrator but i am struggling to learn photoshop. I try to learn by ‘replicating’ styles I see online. I wanted to try this style and i thought it would be very straight forward, but to my disappointment it doesn’t feel right, no matter how i try it. I tried looking for some tutorials online but i couldn’t find anything, so i’m posting it here to see if some of you might know what i am doing wrong.

r/graphic_design - How to do this line texture style?

The style i’m trying to replicate

Source: https://twitter.com/team_empire

I started in Illustrator by placing a vertical line. I then copied it across several times until it filled the screen. I group those lines into one layer and duplicated the layer twice. I rotated one of the copies +60 degrees and one -60 degrees. I then Applied some brush strokes to the lines, to make them ‘feel’ hand drawn. Due to the different strokes, they also appear ‘bolder’ or ‘thicker’ than eachother which i like and the inspiration also has. I then mesh warped the groups (all at the same time to have the intersection angles between the lines stay consistent). which got me this:

r/graphic_design - How to do this line texture style?

Illustrator end result

I exported the three layers separately so i could pick and choose where to have all three and where to only have one or two with the brush tool in Photoshop.

In Photoshop i started with the artwork that i wanted to transform into this style:

r/graphic_design - How to do this line texture style?

OG Artwork. https://valorant.fandom.com/wiki/Omen

I gave it a quick background colour and then I put a slight texture over it using Overlay. I then put my three line texture layers from Illustrator over it and I put a gradient map over it. I used the brush to pick and choose the amount of lines i wanted, like explained before. I tried playing around with some other things too, but the best result i got was this:

r/graphic_design - How to do this line texture style?

Best result, not great…

I also tried the lines on a white background like seen in the reference image. That looked like this:

r/graphic_design - How to do this line texture style?

White background

This one doesn’t look too bad, but it doesn’t really look like the reference. Does anyone know what i am doing wrong? If you have some quick tips, please tell me 🙂

Source link

r/graphic_design - Computer Servicing IT Branding Logo

Computer Servicing IT Branding Logo : graphic_design

World’s largest giant companies make their branding logo in a unique, modern & professional look most out of them. This logo is created for a computer IT servicing brand shop which is providing computer accessories, computer servicing, and selling laptop components. The brand color is blue and green and that’s why I tried to give a feel of the combination of blue and green. The most recognizable customers of this brand are the corporate holders.

r/graphic_design - Computer Servicing IT Branding Logo

Source link

Vote on the best logo for a software review site!

Vote on the best logo for a software review site!

Vote on the best logo for a software review site!

submitted by /u/IneedyourKNEES

Source link

Formula Tracking Demo

How I Visualize My Spreadsheet Formula Dependencies in a Cha…


The formula is the heart of an Excel file. And of course, we all want to do correct calculations and deliver accurate results. When there is something wrong, we want to trace back to those referenced cells to find the root cause. Excel natively has a built-in convenient formula dependent/precedent trace feature, it highlights the dependent/precedent cells and displays arrows to indicate the relationships. This helps users to trace back and find any error formulas easily.

Now, when bringing the spreadsheet online, we can do more. We can display the relationships in a custom way that is most useful according to the application context, or bring them to a different service or application for doing the validation programmatically.

In this article, I am taking the Keikai spreadsheet, ZK Listbox, and ZK Charts components to demonstrate how you can display formula precedent/dependent relationships in a table or in a chart.

What to Build

My goal is to list the precedent and dependent cells of a target cell and show them in 2 different forms: a list and a network graph. Here’s a short demo:

Formula Tracking Demo

Click an item in the list and it will jump to the corresponding cell:

List Selection Demo

Let me explain how I build this application.

Design the UI

Keikai is a spreadsheet component based on the ZK framework, a Java EE UI framework, by which I can build UI by composing various existing components. To start I just need to create a zul file, written in XML format. When a user visits this zul file, ZK will translate this zul into an HTML page with CSS and javascript widgets.

First, I put Keikai on the page. Each tag represents a component and its attributes determine its functions and appearance. Please check Developer Reference for details.

Then add a block to show the cell to trace its formula. ZK allows you to mix HTML tags with ZK tags by declaring a namespace xmlns:h="native". For a UI that doesn’t need to change dynamically, I can just use HTML to save memory.

Next, put 2 listboxes to show precedents and dependents respectively.

  • Line 1: <window> is a container to group components in a window-style box. (See demo.)
  • Line 3: <listbox> is used to show a list of items. It accepts a data model and renders the data model with a template. (See demo.)
  • Line 7: You can design a template to render each object in the data model.

To show precedents and dependents in a network graph, I need to add a chart component:

Finally, arrange them in the horizontal layout:

ZK provides various layout components, I just go with the one that’s most suitable here.

How to Implement Application Logic

After building the UI, I need to add application logic. For example, when clicking the ‘trace’ button, I wish to show precedents and dependents in listboxes. To do this I need to create a controller.

MVC Pattern

ZK supports MVC pattern: Model-View-Controller. The zul plays ‘View.’ It provides a SelectorComposer to play ‘Controller.’ Hence, I need to create a class that extends SelectorComposer and associate the zul with the controller with apply attribute:

Control Components by Java

In the ZK MVC pattern, I can control a component by calling its API. When a user visits a zul page, ZK will create each component’s Java object on the zul, and the controller can get component references with @Wire and selector syntax. Therefore, I don’t need to instantiate each component like  new Spreadsheet() by myself. Here are some examples:

  • Line 3: The default case, wire by component ID as the variable name,spreadsheet. In the zul, there should be <spreadsheet id="spreadsheet"/>.
  • Line 6: Wire a component that is a child of window and has a CSS class precedentBox.

For complete syntax, please refer to JavaDoc.

Initialize Components

After components are created, sometimes we want to initialize them, e.g., configure chart options, then I have to override a lifecycle method doAfterCompose() and implement my initializing logic:

  • Line 10: ZK will call this method after all components are created. (see JavaDoc.)
  • Line 13~19: Initialize chart options.

Event-Driven Programming Model

ZK framework supports the event-driven programming model. Each component fires different events when users interact with them e.g. clicking a button or selecting an item. After applying a controller, I can register event listeners in a controller to listen to events triggered by user action and implement application logic in an event listener. To register an event listener method, I have to put @Listen on a method in a controller like:

  • Line 2: It registers a listener for ON_CLICK event on the component whose ID is trace. Please read Wire Event Listeners for details.

Get Precedents/Dependents

To know what precedent and dependent are, please read the Microsoft Office doc. Keikai provides methods to get them:

These 2 methods return a set of Range , so that I can display them in 2 different forms: a list and a network chart.

Trace in a List

Because Listbox supports model-driven rendering which is also in MVC architecture:

Listbox MVC Architecture

To show precedents/dependents in a Listbox, I put them into a ListModelList and pass it to Listbox.setModel(). Then ListModelList will automatically notify a Listbox to render items in a browser when I add remove Range.

  • Line 7~8: I can assign an empty ListModelList to Listbox first and add items later.
  • Line 19~20: put precedents and dependents into a ListModelList, this will automatically notify a Listbox to render them into browsers.

Trace in a Network Graph

A chart also supports model-driven rendering, what I need to do is similar: add data points into a chart, and the chart will render points into a browser.

  • Line 6, 16: getSeries() and getSeries(1) returns 2 series respectively, so I can give them different colors.

I can trace to 2nd level of precedents and dependents:

Here is an example for Input!J15.

Chart Example Input!J15

Jump to a Precedent/Dependent

When tracing a formula’s precedent, it’s quite convenient to jump to the precedent cell and keep inspecting the next precedents and repeat until we find where is wrong. The same rule applies to dependents. Therefore, I implement a feature: click an item (cell) in Listbox and jump to the cell.

Forward an Event

Because a Listbox is empty and doesn’t have any Listitem when the page is loaded, a controller can’t register a listener on a non-existing component. So I have to forward onClick event to an existed component, Window and register a listener on it.

  • Line 6: forward onClick to its ancestor Window whose ID is rangeWinwith a custom event name onFocusCell

Focus a Cell

  • Line 1: Listen to the custom event onFocusCell specified in a zul (see the previous section).
  • Line 7: focusTo() will scroll a sheet and focus on the specified cell.


Spreadsheets are no doubts one of the most popular business applications in the world. When spreadsheets are brought online, we can do more than just duplicating Excel’s functionality. This example combines the modern web UI components with the classic spreadsheet to demonstrate how you can display formula dependencies in a modern and custom way.  The complete source code can be found at Github.

Source link

r/graphic_design - Finding keywords for aesthetic

Finding keywords for aesthetic : graphic_design

I am currently exploring new aesthetics and art styles to work with and I have come across many on pinterest that I wish to take inspiration from but I am unsure of the keywords I shld be typing to find them? Can anyone help me? The first one, is that blur effect that you find on old films and prints. Im not sure if i shld be searching 90s or even earlier and if there is even a name for it. The second I have attached a photo below as it is harder to describe :

r/graphic_design - Finding keywords for aesthetic

Any help I can get would be appreciated :)) thank you

Source link

r/graphic_design - First edit in photoshop since junior high, looking for feedback

First edit in photoshop since junior high, looking for feedb…

Hello. We were taught photoshop during 8th grade and it was the only time I had engaged with the software. Recently I began getting interested in graphic design, so I tried to make one.

Can anyone give me some general feedback? Thank you very much!

P.S. I know that the text at the very bottom isn’t centered. Somehow throughout the process I clicked something inadvertently and suddenly they all merged into the background, so I couldn’t edit anything anymore.

r/graphic_design - First edit in photoshop since junior high, looking for feedback

Source link

Hot to Try C# 9.0

The Features of C# 9 That Will Make Your Life Easier [Snippe…

This article is a sort of ‘cheat sheet’ for developers on the most useful features of C# 9 and several functions from previous versions. With each new version of C#, its developers strive to make the programming process more convenient and concise. This time around, most of the attention was paid to changes in object properties, the new Record type, and more, but first things first.

C# is a programming language that Microsoft created for their own projects. Its syntactic capabilities have something in common with Java and C++. In 2000, the company’s engineers developed the ASP.NET active server page technology, which allowed databases to be tied to web applications. ASP.NET itself was written in C#. The ability to build flexible and scalable applications in the future is one of the nice advantages of C#. Products can also be very different — from games to web services.

Since 2017, its developers have been announcing a new version of C# year after year. Early on, it was presented as an only object-oriented language, then, in recent years, they added features that brought a functional approach to it. Thus, developers have more variability in solving problems.

In this article, we will analyze the additional features in C# 9 and recall the old features that can also be useful.

Hot to Try C# 9.0

Init-Only Setter

This setter has been lacking for a long time. It was added so that it does not limit the user in their ability to create objects. Init-only setter allows you to initialize properties only in the class constructor or use the object initialization block. None of the previously presented setters could implement this functionality.

What other important features came with the Init-only setter? If your object has, for example, such properties, it will not be mutable. That means you can only change the object at the stage of its creation. Object initializers and constructors are good for creating nested objects where an entire tree of objects is created in a single go. They free the user from writing numerous boilerplates. It is enough to prescribe certain properties.

Deconstruct Feature

A deconstructor implies decomposing an object. It allows you to immediately decompose an object in one line into several variables, declare them in scope, and assign certain values. How do you implement this feature? In the class in which you want this feature to appear, define a deconstructor method. Then set the out parameters in these methods and cast them to the scope that caused this deconstruction. Deconstructors can be overridden. They can have two or more parameters.

A deconstructor implies decomposing an object. It allows you to immediately decompose an object in one line into several variables, declare them in scope and assign certain values. How do you implement this feature? In the class in which you want this feature to appear, define a deconstructor method. Then set the out parameters in these methods and cast them to the scope that caused this deconstruction. Deconstructors can be overridden. They can have two or more parameters.

Imagine a situation: there is one deconstructor with three arguments, and we only need the first and the last. There are two options for how to get them. The first is to overload the deconstructor and make it with two arguments. The second is to use the (_) operator. It will allow us to highlight the variable that we are not passing to the external context. Thus, we will be able to get only the necessary data and not overload the deconstructors.

Indices and Ranges

The index relative to the end of the code array was added in the C# 7 version. This is a handy feature when you need to work, not with the beginning of an array, but with the end. Ranges make it easy to get a sub-array from a shared array.

New Operator

When creating an object, we have the left and right sides of the expression. On the left, we declare the type and name of the variable, and, on the right, we directly create an object. With the var statement on the left, we can omit the type. During compilation, it will pull up from the right side and replace var with the desired data type. This is how var works. Now with the new operator, we only specify the expected data type only on the left side of the expression and omit it on the right.

Let’s say we have a user that is created with three different constructors: empty, with two arguments and using the code initialization block. If we specify the type name on the left side of the expression, we can use the new() operator so as not to indicate the type on the right side.

Also, see how it is now easier to create a Dictionary using the new operator:

Local Functions

Local functions were introduced in #8. They can be declared inside a function, in an expression, or in a constructor. It is convenient to work with these functions in recursion when you need to calculate the degree, factorial, or find the Fibonacci number. In the new version, local functions have been slightly updated. Attributes can now be used with them.

If a method performs several tasks, when you look at it, sometimes you will not immediately understand what it does and what parts it consists of. Local functions allow you to take out the blocks of the algorithm and somehow designate them (give them a name). When our blocks of code are split into local methods and names are given to these methods, it’s easier to navigate the code. If local functions are regularly repeated, I advise you to move them to another class or method and then use them separately.

From personal experience, it is still difficult for me to imagine the use of local functions on a project scale. I think that when developing, you need to keep the project in a consistent style. If it grows over time, local functions can make your code harder to read. In small and isolated solutions, maybe these features will come in handy. For example, Azure Functions, AWS Lambdas, and background workers. But I see no problem in using a private method or extension method instead of a local function.

Top-Level Statement

A top-level statement allows you to remove the clutter of unnecessary code. When creating a console application, we have a program .cs file with a standard set of code (array using, namespace, class program, Main method). In fact, they are not informative, because you still have to write all the code inside the Main function. When a newbie opens a console application and sees many lines of code, he gets confused and doesn’t understand how they work. Fortunately, the .NET developers simplified this point. Now the user writes the application from scratch. Neither namespaces, nor programs, nor Main interfere. There is no need to waste time maintaining this cumbersome infrastructure. However, this applies to small test tasks that do not require a deep understanding of the platform.

Record Type Feature: The Main C# 9 Feature

It is a new data type that borrows several features from value and reference types. The Record is a reference type. However, no object reference is passed during an assignment. The object is copied, like for value types. The record keyword gives this class additional behavior. The major difference is that the Record type has a structured approach to comparing objects. If we have two instances of a class and we compare them, then this happens by reference, not by its properties. While with Record, they are compared by the values of the fields that are inside. Also, when you declare a Record, it creates a set of methods under the hood.

What is already implemented in the Record type:

  • Redefined GetHashCode and Copy and Clone methods.
  • Overridden by ToString.
  • Have a short way of writing.
  • Have Deconstruction by default.
  • It is possible to use the new keyword with when copying.

Records initialization

Let’s see what the Record declaration looks like. There is an access identifier — public. The class is now replaced with the keyword record, followed by its name. Reminiscent of a constructor that takes two arguments. But what will it all turn into then? We will have two fields FirstName and LastName, which will have getters and Init-only setters. Here, we cannot use the object initialization block, because, under the hood, we have overridden the constructor. We can no longer use the default constructor. Now you need to declare it or use the default constructor.

Let’s compare the two records. You can see that the FirstName and LastName of these objects are the same. Therefore, when comparing it returns true. This happens based on the comparison of field values, not references, as in the classes. Let’s create the same user class. When comparing two objects with different references and the same values, we get false.

Since ToString is now redefined: when outputting Record, a construction with all the ‘internals’ of an object is obtained: the type name, property, and nothing needs to be redefined.

We intentionally made records immutable. Instead, we create a new instance with different values. With-expressions are already included here.


When we create a user’s Record using a short type, all of our properties have getters and Init-only setters. What does this mean? This means that the properties will only change during the creation of the object. It rarely works out that one object lives quietly in the application all the time. We need to change something in it. For this, we created the With-expressions construction. They use the syntax of an object initializer and show what exactly is different in the new object from the old one. At its core, With-expression is copying an object like a value type. During copying, it makes it possible to change the values of some object properties that should apply to a new variable, while not affecting the value of an existing variable. This function allows us to change fields and write them to a new object.

Here, we have a user with two fields — FirstName and LastName. We assigned them some values and now we want to write/copy the user to a new variable and then make changes. To do this, after the user, we write the With keyword, and then we can change any fields.

Compared to the previous two versions, the update to C# 9 hasn’t been all that enormous. I can draw the following analogy: if they started the car earlier, then its separate mechanisms are already being completed. Somewhere tweaked the running gear, somewhere — repaid the engine. These features are worth trying on the project, at least in order to understand if they fit.

Source link