<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C# Archives - Pontis Technology</title>
	<atom:link href="https://pontistechnology.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>https://pontistechnology.com/tag/c/</link>
	<description></description>
	<lastBuildDate>Mon, 28 Oct 2024 14:28:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://pontistechnology.com/wp-content/uploads/2023/05/cropped-MicrosoftTeams-image-1-32x32.png</url>
	<title>C# Archives - Pontis Technology</title>
	<link>https://pontistechnology.com/tag/c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What are Shadow Properties in Entity Framework</title>
		<link>https://pontistechnology.com/shadow-properties-in-entity-framework/</link>
					<comments>https://pontistechnology.com/shadow-properties-in-entity-framework/#respond</comments>
		
		<dc:creator><![CDATA[Amor Osmić]]></dc:creator>
		<pubDate>Tue, 13 Jun 2023 07:01:34 +0000</pubDate>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<guid isPermaLink="false">https://pontistechnology.com/?p=9215</guid>

					<description><![CDATA[<p>Greetings weary travelers!Have you heard about Shadow Properties that are part of the Entity Framework? What are shadow properties?Shadow properties are properties that aren&#8217;t defined in your .NET entity class but are defined for that entity type in the Entity Framework Core model.The value and state of these properties are maintained purely in the Change [&#8230;]</p>
<p>The post <a href="https://pontistechnology.com/shadow-properties-in-entity-framework/">What are Shadow Properties in Entity Framework</a> appeared first on <a href="https://pontistechnology.com">Pontis Technology</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Greetings weary travelers!<br>Have you heard about Shadow Properties that are part of the Entity Framework?<br><br><strong>What are shadow properties?</strong><br>Shadow properties are properties that aren&#8217;t defined in your .NET entity class but are defined for that entity type in the Entity Framework Core model.<br>The value and state of these properties are maintained purely in the Change Tracker.<br>Shadow properties are useful when there&#8217;s data in the database that shouldn&#8217;t be exposed on the mapped entity types.<br></p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="553" height="717" src="https://pontistechnology.com/wp-content/uploads/2023/06/1-1.png?x92098" alt="" class="wp-image-9222" srcset="https://pontistechnology.com/wp-content/uploads/2023/06/1-1.png 553w, https://pontistechnology.com/wp-content/uploads/2023/06/1-1-231x300.png 231w" sizes="(max-width: 553px) 100vw, 553px" /></figure>



<p class="wp-block-paragraph"><br><strong>Example</strong><br>One interesting way that Entity Framework uses shadow properties is in a <strong>one-to-many </strong>relationship.<br>Let&#8217;s take a look at an example. (images shown below)<br><br>We have two entity classes, a Student entity and a Course entity.<br>In our case, one student can attend only one course but a course can have more than one student.<br>The entity class representation will be shown in the images below.<br>If we create the migrations, update the database and check the Students table, we can see a column named &#8220;CourseId&#8221;.<br>Entity Framework creates that column and uses it as a shadow property. That column isn&#8217;t exposed in the Student class.<br><br>If we now add students to a course, the CourseId of those students will be updated accordingly.<br>This way Entity Framework knows which student is part of which course without exposing the CourseId property in the Student class.<br></p>



<figure class="wp-block-image size-full"><img decoding="async" width="554" height="717" src="https://pontistechnology.com/wp-content/uploads/2023/06/2-1.png?x92098" alt="" class="wp-image-9223" srcset="https://pontistechnology.com/wp-content/uploads/2023/06/2-1.png 554w, https://pontistechnology.com/wp-content/uploads/2023/06/2-1-232x300.png 232w" sizes="(max-width: 554px) 100vw, 554px" /></figure>



<p class="wp-block-paragraph"><br><strong>Can we add custom shadow properties?</strong><br>We can do that by using the FluentAPI and overriding the OnModelCreating method like this:<br><br>modelBuilder.Entity&lt;Student&gt;().Property&lt;string&gt;(&#8220;ShadowProperty&#8221;);<br><br>We add a new migration, update the database and we can see that a new column, &#8220;ShadowProperty&#8221; has been added to the Students table but it isn&#8217;t present on the Student model.<br><br>Can we access the value of the shadow property?<br>Yes, we can. Shadow property values can be obtained and changed through the ChangeTracker API like this:<br><br>Student student = dbContext.Students.First();<br>dbContext.Entry(student).Property(&#8220;ShadowProperty&#8221;).CurrentValue = &#8220;It&#8217;s a shadow property!&#8221;;<br><br>They can also be referenced in LINQ queries via the&nbsp;EF.Property&nbsp;static method:<br><br>ICollection&lt;Student&gt; students = dbContext.Students.Where(student =&gt;&nbsp;EF.Property<a href="http://ef.property/"></a>&lt;string&gt;(student, &#8220;ShadowProperty&#8221;) != null).ToArray();<br></p>



<figure class="wp-block-image size-full"><img decoding="async" width="554" height="713" src="https://pontistechnology.com/wp-content/uploads/2023/06/3-1.png?x92098" alt="" class="wp-image-9224" srcset="https://pontistechnology.com/wp-content/uploads/2023/06/3-1.png 554w, https://pontistechnology.com/wp-content/uploads/2023/06/3-1-233x300.png 233w" sizes="(max-width: 554px) 100vw, 554px" /></figure>



<p class="wp-block-paragraph"><br><strong>TLDR;</strong> Shadow properties are useful when there&#8217;s data in the database that shouldn&#8217;t be exposed on the mapped entity types. We can add custom shadow properties by using the FluentAPI and overriding the OnModelCreating method.<br>The value of the property can be obtained and changed through the ChangeTracker API, and shadow properties can be referenced in LINQ queries via the EF.Property static method.<br></p>



<p class="wp-block-paragraph">Was this helpful?<br>If you still need&nbsp;<strong>expert help</strong>, leave us a message<strong>&nbsp;<a href="https://pontistechnology.com/home/contact-us/">here</a>&nbsp;</strong>and tell us what’s bothering you.</p>



<p class="wp-block-paragraph"></p>
<p>The post <a href="https://pontistechnology.com/shadow-properties-in-entity-framework/">What are Shadow Properties in Entity Framework</a> appeared first on <a href="https://pontistechnology.com">Pontis Technology</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://pontistechnology.com/shadow-properties-in-entity-framework/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>A quick guide through Tasks and ValueTasks in C#</title>
		<link>https://pontistechnology.com/quick-guide-through-tasks-and-valuetasks-in-c/</link>
					<comments>https://pontistechnology.com/quick-guide-through-tasks-and-valuetasks-in-c/#respond</comments>
		
		<dc:creator><![CDATA[Amor Osmić]]></dc:creator>
		<pubDate>Wed, 24 May 2023 08:33:00 +0000</pubDate>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<guid isPermaLink="false">https://pontistechnology.com/?p=9075</guid>

					<description><![CDATA[<p>Asynchronous programming plays a vital role in modern software development, allowing us to efficiently utilize system resources and create responsive user interfaces. In the realm of C#, two powerful tools that facilitate asynchronous operations are Tasks and ValueTasks. In this blog post, we&#8217;ll have a quick overview of Tasks and ValueTasks in C#. Prepare to [&#8230;]</p>
<p>The post <a href="https://pontistechnology.com/quick-guide-through-tasks-and-valuetasks-in-c/">A quick guide through Tasks and ValueTasks in C#</a> appeared first on <a href="https://pontistechnology.com">Pontis Technology</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph"><strong>Asynchronous programming</strong> plays a vital role in modern software development, allowing us to efficiently utilize system resources and create responsive user interfaces. In the realm of C#, two powerful tools that facilitate asynchronous operations are Tasks and ValueTasks.</p>



<p class="has-medium-font-size wp-block-paragraph">In this blog post, we&#8217;ll have a quick overview of <strong>Tasks</strong> and <strong>ValueTasks</strong> in <strong>C#</strong>. Prepare to optimize your code and unlock the full potential of asynchronous programming!</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="1024" src="https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1-1024x1024.jpg?x92098" alt="" class="wp-image-9082" srcset="https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1-1024x1024.jpg 1024w, https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1-300x300.jpg 300w, https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1-150x150.jpg 150w, https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1-768x768.jpg 768w, https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1-600x600.jpg 600w, https://pontistechnology.com/wp-content/uploads/2023/05/Amor-tasks-1.jpg 1200w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<div style="height:47px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph"><strong>What is a task?</strong><br>In short, a Task is a class that is used for asynchronous programming to execute a single operation. If the operation needs to return a result, the class Task&lt;TResult> is used.<br><br><strong>What is a ValueTask?</strong><br>A ValueTask is a struct wrapper for the Task class. It basically has two fields, a field to store the Task/Task&lt;TResult> and a field to store the TResult.<br><br><strong>The difference</strong><br>Since Task is a class, when it&#8217;s instantiated, it is stored on the heap. That means that, eventually, the garbage collector will be called to free the memory of the instantiated Task class after use.<br><br>ValueTask is a struct and, therefore, is stored on the stack. The benefit of this is that, as soon as the ValueTask is used, it is removed from the stack and that memory is freed.<br><br><strong>When should ValueTask be used instead of Task?</strong><br>ValueTask should be used only when you expect an asynchronous method to run synchronously most of the time.<br><br>In the image provided as part of this post, you can see the code and the benchmark results for the same operation performed by the Task and ValueTask. We can see that, in this case, less memory is used by using ValueTask.<br><br>There are a few guidelines to follow when using ValueTask:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">ValueTask is a one-and-done deal. Don&#8217;t await the same ValueTask more than once as it may result in undefined behavior.</li>



<li class="has-medium-font-size">ValueTask cannot be used with WhenAny(), WhenAll() methods. For that to work, we have to convert the ValueTask into a Task using AsTask().</li>



<li class="has-medium-font-size">The same instance should not be awaited concurrently.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph"><strong>Tldr;</strong> ValueTask is good when used for asynchronous methods that will run synchronously most of the time since it can save memory. But it shouldn&#8217;t be awaited multiple times, it cannot be used with WhenAll(), WhenAny() methods and the same instance should not be awaited concurrently.<br><br>Hopefully this has been helpful to you.<br>However, if you still need <strong>expert help</strong>, leave us a message<strong> <a href="https://pontistechnology.com/home/contact-us/">here</a> </strong>and tell us what&#8217;s bothering you.</p>
<p>The post <a href="https://pontistechnology.com/quick-guide-through-tasks-and-valuetasks-in-c/">A quick guide through Tasks and ValueTasks in C#</a> appeared first on <a href="https://pontistechnology.com">Pontis Technology</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://pontistechnology.com/quick-guide-through-tasks-and-valuetasks-in-c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Object Caching 49/104 objects using Memcached
Page Caching using Disk: Enhanced 
Database Caching 8/24 queries in 0.051 seconds using Memcached

Served from: pontistechnology.com @ 2026-10-30 00:32:49 by W3 Total Cache
-->