C# Course – Codemeets– 02

Today, we will cover Variables, Data Types, Strings, and Working with Numbers in C# console applications. These fundamental concepts help build a strong foundation in C# programming.

C# Variables

A variable is a symbol that serves as a container for storing different values or expressions. It is often used to represent an arbitrary element within a set.

You now have a broader understanding. about what a variable is. So let us explain by c #. See below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String chaName = "Harry";
int age = 30;

Console.WriteLine("my name is " + chaName);
Console.WriteLine("I am " + age);
Console.WriteLine("I like " + chaName);
Console.WriteLine("good for " + age);
Console.ReadLine();

}
}
}
output

This code Output is My Name is Harry. I am 30. I like Harry. good for 30. You can try this code your pc.

C# Data Types

In C#, variables must be declared with a specific data type that the computer recognizes. Since C# is a case-sensitive language, variable names must be used consistently.

Data Types in C#:

1. Primitive Types (Value Types)

These types store data directly in memory and include:

  • Integer Types: byte, sbyte, short, ushort, int, uint, long, ulong
  • Floating-Point Types: float, double, decimal
  • Boolean Type: bool
  • Character Type: char

2. Non-Primitive Types (Reference Types)

These store a reference to the memory location instead of the actual data:

  • String Type: string
  • Object Type: object
  • Dynamic Type: dynamic

Each data type serves a specific purpose, allowing efficient memory management and type safety in C# programming

Data TypeSyntax
StringString name = "Hello";
Charchar grade = 'A';
intint age = 30;
floatfloat gpa = 3.2f;
doubledouble gpa = 2.35686;
boolisMale = true;

Data Types

Strings

Strings handle many tasks efficiently and elegantly. They are especially useful for operations like modifying a name. Refer to the table below for a better understanding.

SyntaxOutput
Console.WriteLine ("hello world");hello world
Console.WriteLine ("hello \n world");hello
world
Console.WriteLine ("hello\"world");hello”world
String Ad = "hello" + "boy";
Console.WriteLine (Ad);
hello boy
String Ad = "Apple";
Console.WriteLine(Ad.Length);
5
Console.WriteLine(Ad.ToUpper());APPLE
Console.WriteLine(Ad.ToLower);apple
Console.WriteLine(Ad.Contains(“hp”);false
Console.WriteLine(Ad.Contains(“Apple”);true
Console.WriteLine(Ad.[3]);L
Console.WriteLine(Ad.Indexof(‘l’));3
Console.WriteLine(Ad.Substring(2));ple

Use for String

Work With Numbers

This helps us understand how code manipulates numbers in different ways. Refer to the table below for a clear idea of various numerical operations C sharp code.

SyntaxOutput
Console.WriteLine(5+7);12
Console.WriteLine(Math.Abs (-40));40
Console.WriteLine(Math.Pow (4,2));16
Console.WriteLine(Math.Sqrt (144));12
Console.WriteLine(Math.Max (10,20));20
Console.WriteLine(Math.Min (10,20));10
Console.WriteLine(Math.Round(4.2));4
Console.WriteLine(Math.Sin(55));-0.99975517335862

Work with Numbers

More about Work with Numbers. example works.

SyntaxOutput
Console.WriteLine(8+6);14
Console.WriteLine(152-56);96
Console.WriteLine(80/5);16
Console.WriteLine(8%3);2
Console.WriteLine(8*3);24
Console.WriteLine((8+9)*(8+2));170

Operations

Now you have the basic knowledge of C sharp code. You can get a better understanding by trying the code again.

Thank for all!

More C# ClickHere!

See you Next C# Lesson Soon!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »