Professional Documents
Culture Documents
Variables :
A variable is a name given to a storage area that is used to store values of various atatypes.
Each variable in C# needs to have a specific type, which determines the size and layout of
the variable's memory.
using System;
class Program
{
static void Main(string[] args)
{
String message="The value is ";
Int32 val=30;
Console.Write(message+val);
Console.ReadKey();
}
}
Data Types :
The C# language comes with a set of Basic data types. These data types are used to build
values which are used within an application.
Array :
Arrays are used to store multiple values in a single variable.To declare an array, define
the variable type with square brackets( [] ).
using System;
class Program
{
static void Main(string[] args)
{
int[] myNumbers = {5, 1, 8, 9};
Console.WriteLine(myNumbers.Max()); // largest value
Console.WriteLine(myNumbers.Min()); // smallest value
Console.WriteLine(myNumbers.Sum()); // sum of myNumbers
}
}
List :
List<T> class represents the list of objects which can be accessed by index. It comes
under the System.Collection.Generic namespace. List class can be used to create a
collection of different types like integers, strings etc.
// C# program to create a List<T>
using System;
using System.Collections.Generic;
class ListExample {
// Main Method
public static void Main(String[] args)
{
// Creating a List of integers
List<int> firstlist = new List<int>();
Function :
Function is a block of code that has a signature. Function is used to execute statements
specified in the code block. A function consists of the following components:
Function name: It is a unique name that is used to make Function call.
Return type: It is used to specify the data type of function return value.
using System;
namespace FunctionExample
{
class Program
{
public void Show(string message)
{
Console.WriteLine("Hello " + message);
}
static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.Show("Monika Jadhav"); // Calling Function
}
}
}
Function Overloading :
using System;
class GFG {
int sum = a + b;
return sum;
int sum = a + b + c;
return sum;
}
Class and Object :
Class and Object are the basic concepts of Object Oriented Programming which revolve
around the real-life entities. A class is a user-defined blueprint or prototype from which
objects are created. Basically, a class combines the fields and methods(member function
which defines actions) into a single unit.
using System;
class MyClass {
int a = x;
int b = y;
int result = a + b;
return result;
int a = 12;
int b = 23;
using System;
namespace CallByValue
{
class Program
{
public void Show(int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);
}
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(val); // Calling Function by passing value
Console.WriteLine("Value after calling the function " + val);
}
}
}
Call by reference method copies the address of an argument into the formal
parameter. In this method, the address is used to access the actual argument
used in the function call. It means that changes made in the parameter alter the
passing argument.
using System;
namespace CallByReference
{
class Program
{
// User defined function
public void Show(ref int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(ref val); // Calling Function by passing reference
Console.WriteLine("Value after calling the function " + val);
}
}
}
String
String class represents the text as a series of Unicode characters and it is defined
in the .NET base class library. The main use of String class is to provide the
properties and methods so that it becomes easy to work with strings.
The C# string manipulation property, and methods that can handle those such
things can be Length property, ToCharArray(), ToLower(), ToUpper(), IndexOf(),
LastIndexOf(), SubString(), CompareTo(), Trim(), TrimEnd(), TrimStart(),
StartsWith(), EndsWith(), Split(), Insert(), Remove(), and Replace().
String Manipulation in C# Length of a string. ...
Length of a string. ...
ToCharArray() method. ...
IndexOf() and LastIndexOf() methods. ...
ToLower() and ToUpper() methods. ...
TrimStart(), TrimEnd(), and Trim() methods. ...
Substring() method. ...
CompareTo() method. ...
StartsWith() and EndsWith() methods.
Constructor :
A constructor is a special method of the class which gets automatically invoked
whenever an instance of the class is created. Like methods, a constructor also contains
the collection of instructions that are executed at the time of Object creation. It is used
to assign initial values to the data members of the same class
namespace MyApplication
{
class Car
{
public string model;
public string color;
public int year;
C# Inheritance :
In C#, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. ... In C#, the class which inherits the
members of another class is called derived class and the class whose members
are inherited is called base class.
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.honk();
Console.WriteLine(myCar.brand + " " + myCar.modelName);
}
}
Abstract Classes
Abstraction in C# is the process to hide the internal details and showing only the
functionality. The abstract modifier indicates the incomplete implementation. The
keyword abstract is used before the class or method to declare the class or method as
abstract.
usinobj System;
interface IAnimal
class Program
myPig.animalSound();