Obomighie Sophie
4 min readFeb 21, 2022

--

Pangram in C#

What is a Pangram? It is a sentence in which every letter in the alphabet of a particular language is present.

Edo Alphabet
English Alphabet

This means our problem is to identify sentences that have all the letters in the alphabet of any single language of choice.

In this article, I will use the English alphabet as the subject and will use C# to solve the problem.

In the first approach, I begin by defining the class Library I will use. System.Linq is the most relevant here as I will use the static method. Next, I create a public class Pangram namespace. Public because I need to be able to use it across files in this project. It is a class because I want to be able to solve a real-life problem called pangram using code. I name it Pangram out of choice.

Now, I jumped into the Pangram curly braces and declared an alphabet variable. This holds the letters of whatever language of choice and in my case, it is the letters of the English alphabet. I declared “alphabet” as a const(constant) field because alphabet will not change throughout this program. However, if while declaring the alphabet field, I miss a letter or 2 or even use letters or numbers, the program will automatically assume those are all the letters in the alphabet.

Some useful points about Const include:

  • Const is a compile time constant unlike ReadOnly which is a runtime constant.
  • It can be declared inside the method.
  • In const fields, we can only assign values in declaration part unlike ReadOnly in which we can assign values in declaration and in the constructor part.
  • Static cannot be used with static modifiers.
  • Constants can be numbers, Boolean values, strings, or a null reference.
  • Because compilers propagate constants, other code compiled with your libraries will have to be recompiled to see the changes.
  • The static modifier is not allowed in a constant declaration.
  • Beginning with C# 10, interpolated strings may be constants, if all expressions used are also constant strings.
  • This also allows my code run faster because my compiler now knows it will never change. String because it is a string.
  • The type of a constant declaration specifies the type of members that the declaration introduces. The initializer of a constant local or a constant field must be a constant expression that can be implicitly converted to the target type.
  • A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and a null reference.

Static is because I want to be able to call the IsPangram class. Static is a class method in C# (the other is non-static methods). Any normal method is a non-static method.

A static method in C# is a method that keeps only one copy of the method at the Type level, not the object level.

That means, all instances of the class share the same copy of the method and its data.

The last updated value of the method is shared among all objects of that Type.

Static methods are called by using the class name, not the instance of the class. The Console class and its Read and Write methods are an example of static methods. The following code example calls Console.WriteLine and Console.ReadKey methods without creating an instance of the Console class.

Notable Points here are:

  1. A static method can be invoked directly from the class level
  2. A static method does not require any class object
  3. Any main() method is shared through the entire class scope so it always appears with static keyword.
  4. Bool because the question is if it is a pangram or not

String Text serves as an input for IsPangram

Lastly, return alphabet.All(text.ToLower().Contains);

alphabet.all calls the entire string in alphabet then saves it in text, then we convert all the text into small letters.

Another possible solution is this by a friend,

Also available on my blog

Resources

--

--