Nested class is a class declared in another enclosing class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested class.Let us see an example code snippet of nested classes in C# −Exampleclass One { public int val1; public class Two { public int val1; } } class Demo { static void Main() { One a = new One(); a.val1++; One.Two ab = new One.Two(); ... Read More
Naming convetion for classesA class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. The following are the conventions for class names.Pascal CasingThe coding conventions for a class name is the the name of the class names, for example, it should being PascalCasing.public class EmployeeDetails {}Above, the class name EmployeeDetails is in PascalCasing.Noun or Noun PhrasesPrefer adding class names as noun or noun phrases −public class Employee {} Identifier is a name used to identify a class, variable, function, or any other user-defined item.The following are the ... Read More
NullReferenceException is a C# version of NullPointerException. To handle and catch it in C#, use try-catch.The below example shows that a variable is set to null and when we try to print it, it throws an exception that gets caught in the catch −Try { a = null; Console.WriteLine(a); }catch (NullPointerException ex) { Console.WriteLine("Variable is Null!"); }The above will allow the exception to be caught and use catch for it.
For numbers in C#, use the int type. It represents an integer, which is positive or negative whole number.Let us see how to add two integers in C# using mathematical operator + −using System; using System.Linq; class Program { static void Main() { int x = 20; int y = 30; int sum = 0; sum = x + y; Console.WriteLine(sum); } }Now let us learn about the order in which these mathematical operators i.e. operator precedence.Operator precedence determines the grouping ... Read More
Runtime polymorphism has method overriding that is also known as dynamic binding or late binding. It is implemented by abstract classes and virtual functions. Abstract classes contain abstract methods, which are implemented by the derived class.Let us see an example of abstract classes that implement runtime polymorphism and works with Overriding −Exampleusing System; namespace PolymorphismApplication { abstract class Shape { public abstract int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle( int a = 0, int ... Read More
To reverse a string, use the Array.Reverse() method.Set the string you want to reverse −string str = "Amit";In the above method, we have converted the string into character array −char[] ch = str.ToCharArray();Then the Reverse() method is used.Array.Reverse(ch);Exampleusing System; namespace Demo { class Program { static void Main(string[] args) { string str = "Amit"; char[] ch = str.ToCharArray(); Array.Reverse(ch); foreach(var items in ch) { Console.WriteLine(items); } Console.ReadLine(); } } }
With method overloading, you can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.Let us see an example. In this the call would go to the method with a single parameter −Exampleusing System; class Student { static void DisplayMarks(int marks1 = 90) { Console.WriteLine("Method with one parameter!"); } static void DisplayMarks(int marks1, int marks2 = 95) { Console.WriteLine("Method with two parameters!"); } ... Read More
To print your own font in C#, firstly construct −FontFamily objectFont ObjectThe FontFamily object sets the typeface like Arial, TimesNewRoman, etc, whereas the Font object sets the size and style of font.Let us create an Arial font style.FontFamily myFontFamily = new FontFamily("Arial"); Font myFont = new Font( myFontFamily, 20, FontStyle.Bold, GraphicsUnit.Pixel);Above, we have set the FontFamily object. The first parameter passed to the Font() is the FontFamily object “myFontFamily”, then comes the size of the font. The third argument sets the style.
To display single variable value in C#, you just need to use Console.WriteLine()Let us see an example. Here, we have displayed the value of a single variable “a” in a line −Exampleusing System; using System.Linq; class Program { static void Main() { int a = 10; Console.WriteLine("Value: "+a); } }To display multiple variables value in C#, you need to use the comma operator in Console.WriteLine().Let us see an example. Here, we have displayed the value of multiple variables “a” and “b” in a line −Exampleusing System; using System.Linq; class Program ... Read More
Private MethodsTo set private methods, use the private access specifier.Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.Final MethodsFor final methods, use the sealed modifier.When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.Let us see an example −The following example won’t ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP