Types of Number literals in C#

Hi Folks,

In this post I touch briefly on types of number literals in C# programming language.

An illustration is

var f = 0f; // float
var d = 0d; // double
var m = 0m; // decimal (money)
var u = 0u; // unsigned int
var l = 0l; // long
var ul = 0ul; // unsigned long

For example, this menas if you see a number with “m” suffix, it is a decimal literal.

That’s all for now. Till next time, happy software development.

How to assign data to properties when instantiating an object in c#

Hi Folks,

In this post, we talk about how to assign data to properties when instantiating an object in c#.

Suppose you have a class like

 

public class Album 
{
    public string Name {get; set;}
    public string Artist {get; set;}
    public int Year {get; set;}

    public Album()
    { }

    public Album(string name, string artist, int year)
    {
        this.Name = name;
        this.Artist = artist;
        this.Year = year;
    }
}

You can create an instance of Album class either via the constructor e.g.

var albumData = new Album("Albumius")
{
Artist = "Artistus",
Year = 2013
};

After compilation, this is similar to the following:

var albumData = new Album("Albumius");
albumData.Artist = "Artistus";
albumData.Year = 2013;

That’s all for now. Till next time, happy software development.

References

C# : assign data to properties via constructor vs. instantiating. stackoverflow. https://stackoverflow.com/questions/19138195/c-sharp-assign-data-to-properties-via-constructor-vs-instantiating

Verbatim String Literal in C#

Hi Folks,

In this short post I talk about how to write verbatim string literals in C#. You write this by using using the @ keyword just before the string. What does a verbatim string literal mean? It means that the characters in it will be interpreted verbatim. This means that escape sequences such as \ \ for backslash, hexadecimal escape sequences such as \x0041 for uppercase A, and Unicode sequences such as \u0041 for uppercase A, are interpreted literally. To demonstrate this, consider that the following two strings are equivalent.

string filename1 = @"c:\documents\file.txt";
string filename2 = "c:\\documents\\file.text";

That’s all for now. Till next time, happy software development.

 

Reference

1. Verbatim text and strings – @ – C# reference. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim