Skip to content

.NET or C# Reference Links and Code Snippets

Published: at 10:43 AM

As a .NET or C# developer, having access to useful resources and code snippets can significantly enhance your productivity. In this article, we’ll highlight some valuable references and provide examples that will help you streamline your development process.

Here are some of the best resources for finding C# code snippets, learning new techniques, or solving common programming challenges:

a) Extension Methods in C#

Extension methods in C# allow you to add new functionality to existing types without modifying the original source code. This resource provides comprehensive documentation and examples on how to use extension methods effectively in C#.

Example:

Here’s a simple extension method example to extend the string class in C#:

public static class StringExtensions
{
    public static bool IsNullOrEmptyOrWhitespace(this string str)
    {
        return string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str);
    }
}

// Usage
string myString = "   ";
bool isEmptyOrWhitespace = myString.IsNullOrEmptyOrWhitespace(); // returns true

b) JSON Utilities

If you work with JSON data in C#, this site offers useful tools for JSON formatting, validation, and conversion. It’s particularly helpful for developers who frequently parse and manipulate JSON data.

Example:

If you need to deserialize JSON into C# objects, you can use the JsonConvert class from the popular Newtonsoft.Json library.

using Newtonsoft.Json;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

string json = "{\"Name\": \"John\", \"Age\": 30}";
Person person = JsonConvert.DeserializeObject<Person>(json);

// Usage
Console.WriteLine(person.Name);  // Output: John
Console.WriteLine(person.Age);   // Output: 30

2. Common C# Code Snippets

Here are some C# code snippets you can use in various development tasks:

a) String Reversal

public static string ReverseString(string str)
{
    char[] arr = str.ToCharArray();
    Array.Reverse(arr);
    return new string(arr);
}

// Usage
string reversed = ReverseString("hello");
Console.WriteLine(reversed);  // Output: olleh

b) Finding the Largest Number in an Array

public static int FindLargest(int[] numbers)
{
    return numbers.Max();
}

// Usage
int[] numbers = { 1, 5, 3, 9, 2 };
Console.WriteLine(FindLargest(numbers));  // Output: 9

c) Sorting a List of Objects

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void SortPersons(List<Person> persons)
{
    persons.Sort((x, y) => x.Age.CompareTo(y.Age));
}

// Usage
List<Person> persons = new List<Person>
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
    new Person { Name = "Charlie", Age = 20 }
};

SortPersons(persons);
foreach (var person in persons)
{
    Console.WriteLine($"{person.Name}: {person.Age}");
}
// Output:
// Charlie: 20
// Alice: 25
// Bob: 30

3. Working with LINQ in C#

LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to query collections in a more readable and efficient manner.

a) Simple LINQ Query

public static void SimpleLinqQuery()
{
    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    var evenNumbers = from n in numbers
                      where n % 2 == 0
                      select n;

    foreach (var num in evenNumbers)
    {
        Console.WriteLine(num); // Output: 2, 4
    }
}

b) LINQ with Method Syntax

public static void LinqWithMethodSyntax()
{
    List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
    var evenNumbers = numbers.Where(n => n % 2 == 0);

    foreach (var num in evenNumbers)
    {
        Console.WriteLine(num); // Output: 2, 4
    }
}

Whether you’re looking for C# extension method examples, JSON utilities, or simple LINQ queries, these resources and snippets will help you write efficient and cleaner C# code. Keep them handy as you continue building your .NET applications.


Previous Post
Setting Up Pingman on Cron
Next Post
Add FF Dev Ed Shortcut - Linux