Close
Close

C# Input, Keywords and Naming Conventions


Till now, you know how to print any message on the screen. In this chapter, you will learn more about basic syntax of C# and also to take input from a user.

C# Whitespaces


In C#, whitespace refers to spaces, tabs (\t) and newlines (\n). Either of spaces, tabs (\t) or newlines (\n) is not visible on the screen and we can only see blank spaces and thus, the name whitespace. In some places in our code, whitespace is necessary whereas, in other places, it is just given to improve readability. For example, while writing 'class Hello', it is necessary to give a space between class and Hello (as they are two different words).

class Hello

On the contrary, there is no need to give any space in the following statement, after ({).

class Hello{static void Main(string[] args){Console.WriteLine("Hello World");}}

using System;
class Hello
{static void Main(string[] args){Console.WriteLine("Hello World");}}
Output
Hello World

Although we can give as many whitespaces as we want, the compiler ignores all the unnecessary whitespaces. The following code also runs just fine.

using System;
class    Hello{
  static    void           Main(string[] args)
  {
                Console.WriteLine("Hello World");




  }
}
Output
Hello World

In this example, compiler ignored the unnecessary spaces and the code just ran fine.

whitespaces in C#

C# Identifiers


An identifier is the name of any user-defined element. Names of classes, methods, variables, etc. are identifiers. Let's see an example first.

using System;
class Hello
{
  static void Main(string[] args)
  {
      int a = 2;
      Console.WriteLine("Hello World");
  }
}
Output
Hello World
You can ignore any warning for now if you are getting one. It is occuring because we are not using the variable a anywhere in our program after defining it.

int a = 2a is the name of an integer variable which is assigned a value of 2. So, a is an identifier. You will learn about variables in the next chapter. For now, you can just proceed with the knowledge that a is a variable which will store an integer value in it because of int written before it.

variable in C#

The name of the class Hello is also an identifier and so is the name of the method - Main.

identifier in C#

C# is a case-sensitive language i.e., it distinguishes between uppercase and lowercase. For example, hello and Hello are different identifiers in C#.

There are few rules for naming an identifier and we should take care of them while choosing any name. Some of the important rules are:

  • Identifiers must start with a letter or _(underscore). For example, 1a, 2hello, etc. are invalid identifiers.
  • Identifiers should not contain white spaces.
  • Identifiers must contain only alphanumeric characters or _(underscore).
  • Identifiers are case-sensitive. For example, abc and ABC are two different identifiers.
  • Identifiers may contain Unicode characters.

C# Keywords


There are some predefined reserved words in C# which have their special meaning and thus cannot be used as identifiers. These words are called keywords. For example, class is a keyword used to define class, using is used to include a namespace, etc.

We can use a keyword as an identifier by including @ before it. For example, if we want to name an identifier class, we can name it @class.

Some of the common keywords are:

abstract as base
break bool break
byte case catch
char checked class
const continue decimal
default delegate do
double else enum
event explicit extern
false finally fixed
float for foreach
goto if implicit
in int interface
internal is lock
long namespace new
null object operator
override params private
protected public readonly
ref return sbyte
sealed short sizeof
stackalloc static string
struct switch this
throw true try
typeof uint ulong
unchecked unsafe ushort
using using static virtual
void volatile while

You can always look at the official documentation to know more about keyword.

C# Taking Inputs


We make our programs for users. So, there must be some way to involve users in our program and to take input from them. In this section, you will learn to do so.

There are different methods to take input. Let's start learning them.

C# ReadLine


To input some value from a user, we use the ReadLine method same as we use the WriteLine method for the output. Like WriteLine, ReadLine is also defined in the Console class of System namespace.

ReadLine takes input from the user until 'Enter' is pressed.

readline in C#

Look at the following example in which the user is entering some value for a variable which is then printed on the screen.

using System;
class Input
{
  static void Main(string[] args)
  {
      string a;
      a = Console.ReadLine();
      Console.WriteLine(a);
  }
}
Output
Learning C# from CodesDope
Learning C# from CodesDope

Here, string a is just like int a which we used earlier. It means that a is a variable which will store a string value in it. A string is basically a collection of characters. In simple English, it is a word, sentence, etc. For example, "Hello", "CodesDope", "Hello World", "abcd", etc. all are strings.

Console.ReadLine() takes an input from the user and we gave "Learning C# from CodesDope" to it. So, a = Console.ReadLine() is equivalent to a = "Learning C# from CodesDope".

Lastly, we used Console.WriteLine(a) to print the value of a and it printed its value i.e., "Learning C# from CodesDope".

C# Read


We used ReadLine to take input of the entire sentence, Read is used to take input of a single character like, 'a', 'b', etc. However, Read doesn't give us back the character but its ASCII value. In Computer Science, each character has an ASCII value (integer value) associated with it. For example, the ASCII value of 'a' is 97, 'A' is 65, etc. You can take a look at the ASCII chart for ASCII values of different character. Let's look at an example.

using System;
class Hello
{
  static void Main(string[] args)
  {
      int a;
      a = Console.Read();
      Console.WriteLine(a);
  }
}
Output
a97

The ASCII value of a character will be an integer. So, we are using an integer variable (int a) to store the ASCII value of the character given character.

Console.Read doesn't wait for the 'Enter' to be pressed and stops taking input as soon as a character is given to it.

We used Console.WriteLine(a) to print the ASCII value of the given character.

C# Conventions


Did you notice that we are always starting the name of a class with a capital letter like Hello, Print, etc. This is not a rule but a convention we follow. This is important because a programmer writing a code in C# will name his classes, methods, etc. using these conventions and it will be easier to read the program by other programmers if conventions are followed. We are going to give some conventions here and we will give more along with the course.

conventions in C#

  • We use PascalCase for the name of class i.e., if the name of a class is made up of multiple words, the first letter of all the words will be capital. For example, Hello, HelloClass, MyNewClass, etc.
  • We also use PascalCase for the name of a method. For example, Main, MyMethod, MyMethodName, etc.
  • Identifiers should not contain two consecutive _ characters. Those names are reserved for compiler generated identifiers.
  • For Local variables, camelCase is used i.e., the first word will start with small letter and the rest of the words will start with capital letters without any spaces. For example, backgroundColor, numberCount, etc.

As stated above we will introduce more conventions along with this course.


Ask Yours
Post Yours
Doubt? Ask question